app.config不保存值

Jac*_*ack 8 .net c# xml app-config

App.Config是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
  <add key="foo" value=""/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我尝试foo使用以下方法保存值:

private void SaveValue(string value) {
    var config =
        ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    config.AppSettings.Settings.Add("foo", value);
    config.Save(ConfigurationSaveMode.Modified); 
}
Run Code Online (Sandbox Code Playgroud)

但这并没有改变它的价值.而且我没有例外.怎么解决这个问题?提前致谢!

RBD*_*Dev 28

使用Visual Studio进行调试时,可能<yourexe>.vshost.exe.config会修改而不是<yourexe>.exe.config.在发布模式下构建应用程序时,仅<yourexe>.exe.config存在并将更新.

您的代码还将在配置文件中添加一个额外的节点.使用类似下面的代码来更新设置:

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