app.config 不保存值 [英] app.config are not saving the values

查看:36
本文介绍了app.config 不保存值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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 进行调试时,.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:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["foo"].Value = "text";     
config.Save(ConfigurationSaveMode.Modified);

这篇关于app.config 不保存值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆