winform记住密码和判断是否已经记住密码

1 /// <summary>
2 /// 根据传入值记住密码
3 /// </summary>
4 /// <param name="remember">是否记录</param>
5 /// <param name="userName">用户名</param>
6 /// <param name="password">密码</param>
7   private void Remember(bool remember, string userName, string password)
8 {
9 try
10 {
11 RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
12 key = key.CreateSubKey("CheckManage", RegistryKeyPermissionCheck.Default);
13 if (remember)
14 {
15 key.SetValue("username", userName);
16 key.SetValue("password", password);
17 }
18 else
19 {
20 if (key.GetValue("username") != null) key.DeleteValue("username");
21 if (key.GetValue("password") != null) key.DeleteValue("password");
22 }
23 }
24 catch { }
25 }
26
27
28 /// <summary>
29 /// 判断是否被记住密码
30 /// </summary>
31 /// <returns></returns>
32 private bool HasRemember()
33 {
34 try
35 {
36 RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\CheckManage", true);
37 object objU = key.GetValue("username");
38 if (objU != null) { this.txtUserName.Text = objU.ToString(); }
39
40 object objP = key.GetValue("password");
41 if (objP != null) this.txtPassword.Text = objP.ToString();
42 return objU != null && objP != null;
43 }
44 catch { return false; }
45 }

 

posted @ 2010-09-02 10:49  Luoxiaojie  阅读(701)  评论(0)    收藏  举报