c/s项目记住账号密码功能

 
1.login.cs中的一开始加载页面
//从注册表中读取 是否保存了用户名密码 自动启动配置 string RegeditKey = "PMSApp"; RegistryKey location = Registry.LocalMachine; RegistryKey soft = location.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", false);//可写 RegistryKey myPass = soft.OpenSubKey(RegeditKey, false); try { string banben = myPass.GetValue("DisplayVersion").ToString(); lbbanben.Text = "版本号 : " + banben; } catch (Exception ex) { //MessageBox.Show(ex.Message); } try { string s1 = myPass.GetValue("s1").ToString();//s1账号 string s2 = myPass.GetValue("s2").ToString();//s2密码 bool ifSave = Convert.ToBoolean(myPass.GetValue("s3"));//s3判断状态,是否需要启动改功能 checkBox1.IsChecked = ifSave; if (ifSave) { txtAdminPassword.Password = "";//我只需要记住账号所以这个地方可以忽略 txtAdminName.Text = s1; } else { txtAdminPassword.Password = ""; txtAdminName.Text = ""; } // ifFistIn = false; //程序已启动完毕,可以执行注册表相关动作 } catch (Exception ex) { //MessageBox.Show(ex.Message); } 2.记住账号按钮 //记住账号 private void checkBox1_Click(object sender, RoutedEventArgs e) { if ((sender as CheckBox).IsChecked == true) { string RegeditKey = "PMSApp"; RegistryKey location = Registry.LocalMachine; RegistryKey soft = location.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", true);//可写 RegistryKey myPass = soft.CreateSubKey(RegeditKey); myPass.SetValue("s1", txtAdminName.Text); myPass.SetValue("s2", txtAdminPassword.Password); myPass.SetValue("s3", checkBox1.IsChecked); soft.Close(); location.Close(); } else { string RegeditKey = "PMSApp"; RegistryKey location = Registry.LocalMachine; RegistryKey soft = location.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", true);//可写 RegistryKey myPass = soft.CreateSubKey(RegeditKey); myPass.SetValue("s1", txtAdminName.Text); myPass.SetValue("s2", txtAdminPassword.Password); myPass.SetValue("s3", checkBox1.IsChecked); soft.Close(); location.Close(); } } 3.更换账户时,改变记住账号的状态 private void txtAdminName_TextChanged(object sender, TextChangedEventArgs e) { string s1 = ""; try { string RegeditKey = "PMSApp"; RegistryKey location = Registry.LocalMachine; RegistryKey soft = location.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", false);//可写 RegistryKey myPass = soft.OpenSubKey(RegeditKey, false); s1 = myPass.GetValue("s1").ToString(); if (txtAdminName.Text == s1) { checkBox1.IsChecked = true; } else { checkBox1.IsChecked = false; } } catch { } }

 

posted on 2019-05-30 09:27  菜鸟也想上天  阅读(423)  评论(0编辑  收藏  举报