WPF-本地保存登陸帳號密碼

WPF-本地保存登陸帳號密碼

  在登陸界面,記住帳號密碼,固然,前提是正確的帳號和密碼。app

App.config

  在App.config文件,<configuration>中添加如下代碼,其中key是鍵,value是具體保存的值,在這聲明瞭三個,分別是帳號、密碼和是否記住的bool屬性spa

1    <appSettings>
2       <add key="userName" value=""/>
3       <add key="password" value=""/>
4       <add key="isRemind" value=""/>
5     </appSettings>

Methods

  而後就是具體的讀取和寫入方法,在這,寫成了一個公共方法code

 1         /// <summary>
 2         /// 讀取客戶設置
 3         /// </summary>
 4         /// <param name="settingName"></param>
 5         /// <returns></returns>
 6         public static string GetSettingString(string settingName)
 7         {
 8             try
 9             {
10                 string settingString = ConfigurationManager.AppSettings[settingName].ToString();
11                 return settingString;
12             }
13             catch (Exception)
14             {
15                 return null;
16             }
17         }
18 
19         /// <summary>
20         /// 更新設置
21         /// </summary>
22         /// <param name="settingName"></param>
23         /// <param name="valueName"></param>
24         public static void UpdateSettingString(string settingName, string valueName)
25         {
26             Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
27 
28             if (ConfigurationManager.AppSettings[settingName] != null)
29             {
30                 config.AppSettings.Settings.Remove(settingName);
31             }
32             config.AppSettings.Settings.Add(settingName, valueName);
33             config.Save(ConfigurationSaveMode.Modified);
34             ConfigurationManager.RefreshSection("appSettings");
35         }

  在登陸成功後調用修改配置的方法blog

1 UpdateSettingString("userName", LoginName);

  初始化的時候給其賦值string

1 LoginName = GetSettingString("userName");

結束

  整個功能就ok了~io

相關文章
相關標籤/搜索