分类目录

链接

2011 年 6 月
 12345
6789101112
13141516171819
20212223242526
27282930  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
dat保存登陆密码
.NET 暂无评论 阅读(1,721)
  1. winform--记住密码 收藏 
  2.  
  3. 要引用IO
  4.  
  5. private string accountFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +"\\accounts.dat ";
  6.  
  7. private IDictionary<string, string> accounts = new SortedList<string, string>();
  8.  
  9. /// <summary>
  10.         /// 记住密码
  11.         /// </summary>
  12.         private void ReloadAccounts()
  13.         {
  14.             this.txtUserName.AutoCompleteCustomSource.Clear();
  15.             accounts.Clear();
  16.             if (!File.Exists(accountFilePath))
  17.             {
  18.                 return;
  19.             }
  20.             using (StreamReader reader = new StreamReader(accountFilePath))
  21.             {
  22.                 while (true)
  23.                 {
  24.                     string username = reader.ReadLine();
  25.                     if (string.IsNullOrEmpty(username)) break;
  26.                     string password = reader.ReadLine();
  27.                     if (string.IsNullOrEmpty(password)) break;
  28.                     accounts.Add(username, password);
  29.                     txtUserName.AutoCompleteCustomSource.Add(username);
  30.                 }
  31.             }
  32.         }
  33.  
  34. /// <summary>
  35.         /// 登录事件
  36.         /// </summary>
  37.         /// <param name="sender"></param>
  38.         /// <param name="e"></param>
  39.         private void btnLogin_Click(object sender, EventArgs e)
  40.         {
  41.             if (accounts.ContainsKey(this.txtUserName.Text))
  42.             {
  43.                 accounts[this.txtUserName.Text] = this.txtUserPass.Text;
  44.             }
  45.             else
  46.             {
  47.                 accounts.Add(this.txtUserName.Text, this.txtUserPass.Text);
  48.             }
  49.  
  50.             using (StreamWriter writer = new StreamWriter(accountFilePath))
  51.             {
  52.                 foreach (KeyValuePair<string, string> account in accounts)
  53.                 {
  54.                     writer.WriteLine(account.Key);
  55.                     writer.WriteLine(account.Value);
  56.                 }
  57.             }
  58.  
  59.             ReloadAccounts();
  60.  
  61.             string user = this.txtUserName.Text.Trim();  
  62.             //加密
  63.             string bbb = Crypto.EncryptData("jm", this.txtUserPass.Text.Trim());
  64.             string pwd = bbb;
  65.  
  66.             string filter = "username='" + user + "'" + " and password='" + pwd + "'";
  67.  
  68.             DataTable loginTable = manager.selectAll("userlist", "*", filter);
  69.  
  70.             if (this.txtUserName.Text.Trim() == "" || bbb == "")
  71.             {
  72.                 MessageBox.Show("用户名或密码不能为空!");
  73.                 this.txtUserName.Focus();
  74.             }
  75.             else if (loginTable.Rows.Count == 0)
  76.             {
  77.                 MessageBox.Show("用户名或密码错误!");
  78.                 this.txtUserName.Focus();
  79.             }
  80.             else
  81.             {
  82.                 MessageBox.Show("登录成功!");
  83.             }
  84.         }  
  85.  
  86.         //当鼠标离开用户名时自动获得密码
  87.         private void txtUserName_Leave(object sender, EventArgs e)
  88.         {
  89.             string password = string.Empty;
  90.             if (accounts.TryGetValue(this.txtUserName.Text, out password))
  91.                 this.txtUserPass.Text = password;
  92.         }
  93.  

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:dat保存登陆密码 | Bruce's Blog

发表评论

留言无头像?