讀寫 應用程式設定檔 xxx.exe.config
//先加入參考System.Configuration
//再using
using System.Configuration;
//寫入設定檔
protected void SetAppSettings(String key, String value)
{
Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.AppSettings[key] != null)
oConfig.AppSettings.Settings[key].Value = value;
else
oConfig.AppSettings.Settings.Add(key, value);
oConfig.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
//讀取設定檔
protected String GetAppSettings(String key, String defaultValue)
{
if (ConfigurationManager.AppSettings[key] == null) SetAppSettings(key, defaultValue);
return ConfigurationManager.AppSettings[key];
}
//使用
//寫入
SetAppSettings("txtAccount",txtAccount.Text);
//讀取
txtAccount.Text = GetAppSettings("txtAccount", "");