app.config 不保存值
我的 App.Config 类似于:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="foo" value=""/>
</appSettings>
</configuration>
我尝试使用以下方法保存 foo 值:
private void SaveValue(string value) {
var config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("foo", value);
config.Save(ConfigurationSaveMode.Modified);
}
但这不会改变它的值。我也不例外。 如何解决这个问题?提前致谢!
My App.Config
is something like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="foo" value=""/>
</appSettings>
</configuration>
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您使用 Visual Studio 进行调试时,可能会修改
.vshost.exe.config
而不是.exe.config
。当您在发布模式下构建应用程序时,只有.exe.config
存在并将被更新。您的代码还将向配置文件添加一个额外的节点。使用类似下面的代码来更新设置:
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:
App.config 被复制到构建时的输出文件夹,名为
.exe.config
。这是在运行时加载和保存的实际配置文件。查看输出文件夹,您可能会发现配置文件包含您的更改。
App.config is copied to the output folder on build, named
<yourexe>.exe.config
. This is the actual configuration file that is loaded and saved on runtime.Have a look in your output folder, there you will likely find that the configuration file contains your changes.
尝试先删除旧值,然后再次添加
Try by first deleting the old value and then add it again