ConfigurationManager.AppSettings.Settings.Add() 在每个 运行 上附加值
ConfigurationManager.AppSettings.Settings.Add() appends value on each run
我有以下一段代码。每次,我 运行 C# 项目都会附加应用程序设置键的值。
var configSettings = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configSettings.AppSettings.Settings.Add("Key", "Value");
configSettings.Save(ConfigurationSaveMode.Full, true);
ConfigurationManager.RefreshSection("appSettings");
第一 运行:
键:值
第二 运行:
键、值、值
为什么要附加值?我需要它从每个 运行.
的干净盘子开始
您需要检查 AppSetting 是否已经存在。如果存在,则必须更新该值。如果不是,则必须添加该值。
var configSettings = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configSettings.AppSettings.Settings;
if (settings["Key"] == null)
{
settings.Add("Key", "Value");
}
else
{
settings["Key"].Value = "NewValue";
}
configSettings.Save(ConfigurationSaveMode.Full, true);
ConfigurationManager.RefreshSection("appSettings");
AppSettings.Settings
基本上是 key/value 对的集合。
查看以下 MSDN 文档了解更多详细信息。
我有以下一段代码。每次,我 运行 C# 项目都会附加应用程序设置键的值。
var configSettings = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configSettings.AppSettings.Settings.Add("Key", "Value");
configSettings.Save(ConfigurationSaveMode.Full, true);
ConfigurationManager.RefreshSection("appSettings");
第一 运行: 键:值
第二 运行: 键、值、值
为什么要附加值?我需要它从每个 运行.
的干净盘子开始您需要检查 AppSetting 是否已经存在。如果存在,则必须更新该值。如果不是,则必须添加该值。
var configSettings = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configSettings.AppSettings.Settings;
if (settings["Key"] == null)
{
settings.Add("Key", "Value");
}
else
{
settings["Key"].Value = "NewValue";
}
configSettings.Save(ConfigurationSaveMode.Full, true);
ConfigurationManager.RefreshSection("appSettings");
AppSettings.Settings
基本上是 key/value 对的集合。
查看以下 MSDN 文档了解更多详细信息。