c# - app.config 没有保存值

标签 c# .net xml 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); 
}

但这并没有改变它的值(value)。我没有异常(exception)。 如何解决这个问题?提前致谢!

最佳答案

当您使用 Visual Studio 进行调试时,可能是 <yourexe>.vshost.exe.config被修改而不是 <yourexe>.exe.config .当您在 Release模式下构建应用程序时,只有 <yourexe>.exe.config存在并将更新。

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

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

关于c# - app.config 没有保存值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8840904/

相关文章:

c# - System.IO.Directory.Delete方法只是将文件发送到回收站。有什么办法可以永久删除吗?

c# - 使用 MRTK 在 Hololens 中显示天空盒

c# - List.clear() 后跟 List.add() 不起作用

c# - 有没有办法使用 XCode 创建 ASP .NET 文件?

c# - 如何获取特定月份和年份的日期

c# - 为什么我不能在扩展 List 的类中调用 OrderBy?

c# - 将 GeoJSON 响应转换为 FeatureCollection

python - 如何在 python 中读取根 XML 标记

sql-server - SQL Server 将 XML 子节点 append 到父节点

android - 文件过早结束