C# 在c中退出应用程序后未保存appSettings#
我正在尝试在运行时使用ConfigurationManager保存应用程序设置。我使用以下代码更新配置中的键值对:C# 在c中退出应用程序后未保存appSettings#,c#,C#,我正在尝试在运行时使用ConfigurationManager保存应用程序设置。我使用以下代码更新配置中的键值对: private bool SetAppConfigSetting(string Key, string Value) { bool result = false; try { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguratio
private bool SetAppConfigSetting(string Key, string Value)
{
bool result = false;
try
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(Key);
var kvElem = new KeyValueConfigurationElement(Key, Value);
config.AppSettings.Settings.Add(kvElem);
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
result = true;
}
finally
{ }
return result;
}
每次我调用它时,它都会删除现有的键值对,并用新的应用程序设置替换它
但是,我发现只有在我的代码运行时,appSettings
才会在Application.vshost.exe.config
中得到更新,但是只要代码点击Application.Exit()
,那么appSettings
就会全部消失
我哪里出了问题?尝试了此修改,但仍然无效。我发现了问题。这是因为我是在调试模式而不是发布模式下构建代码的。当我将构建模式更改为Release时,我可以看到AppSettings被保存到Application.exe.Config文件中。谢谢!