读:
/// <summary>
/// 获取配置文件AppSeting节点数据信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetAppSettingKeyValue(string key)
{
Configuration cfg = ConfigurationManager.OpenExeConfiguration(@"WinDraw.dll");
return cfg.AppSettings.Settings[key].Value;
}
写:
/// <summary>
/// 保存配置文件AppSeting节点的数据信息
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void SaveAppSettingKeyValue(string key, string value)
{
Configuration cfg = ConfigurationManager.OpenExeConfiguration(@"WinDraw.dll");
bool isExist = false;
foreach (string k in cfg.AppSettings.Settings.AllKeys)
{
if (k == key)//判断是否存在对应的节点
{
isExist = true;
}
}
if (isExist)//如果存在则直接赋值
{
cfg.AppSettings.Settings[key].Value = value;
}
else//不存在就创建
{
cfg.AppSettings.Settings.Add(key, value);
}
cfg.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}