app.config 不保存值 [英] app.config are not saving the values
问题描述
我的 App.Config
类似于:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="foo" value=""/>
</appSettings>
</configuration>
我尝试使用以下方法保存 foo
值:
I try to save the foo
value using the following method:
private void SaveValue(string value) {
var config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("foo", value);
config.Save(ConfigurationSaveMode.Modified);
}
但这不会改变它的价值.我没有例外.如何解决这个问题?提前致谢!
but this not change the value of it. and I don't get a exception. how to fix this? thanks in advance!
推荐答案
当您使用 Visual Studio 进行调试时,
可能被修改而不是
.当您在发布模式下构建应用程序时,只有
存在并且将被更新.
When you are debugging with Visual Studio probably the <yourexe>.vshost.exe.config
is modified instead of the <yourexe>.exe.config
. When you build the application in Release mode only the <yourexe>.exe.config
exists and will be updated.
您的代码还将在配置文件中添加一个额外的节点.使用类似下面的代码来更新设置:
Your code will also add an extra node to the configuration file. Use something like the code below to update the setting:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["foo"].Value = "text";
config.Save(ConfigurationSaveMode.Modified);
这篇关于app.config 不保存值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!