版权声明:欢迎评论和转载,转载请注明来源。 https://blog.csdn.net/zy332719794/article/details/44916535
/// <summary>
/// 读写系统配置xml文件。
/// </summary>
public class ConfigurationFile
{
/// <summary>
/// 写入key值
/// </summary>
public static bool SetKeyValue(string key, string value)
{
//增加的内容写在appSettings段下 <add key="RegCode" value="0"/>
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
try
{
if (config.AppSettings.Settings[key] == null)
{
config.AppSettings.Settings.Add(key, value);
}
else
{
config.AppSettings.Settings[key].Value = value;
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// 读取指定key的值
/// </summary>
public static string GetKeyValue(string key)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
return "";
else
return config.AppSettings.Settings[key].Value;
}
}