c# - 如何更新 .NET 中现有配置键的值?

标签 c# .net config

举个例子...

<add key="IDs" value="001;789;567"/>

如何以编程方式仅更新(追加)App.config 中现有键的值?

<add key="IDs" value="001;789;567;444"/>

我的代码目前有以下添加新 key 的代码,但我不知道如何更新 key 。

System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings.Add(appKey, appKeyValue);

// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);

最佳答案

您可以通过设置键或索引器访问它。

Configuration config = ConfigurationManager.OpenExeConfiguration("YourConfigFilePath");
config.AppSettings.Settings["IDS"].Value = "001;789;567;444";  // Update the value (You could also use your appKey variable rather than the string literal.

config.Save();

作为附加说明,导入 System.Configuration 命名空间可能比每次都使用 System.Configuration 别名更容易。

关于c# - 如何更新 .NET 中现有配置键的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768388/

相关文章:

c# - 定义类的属性

c# - 虚拟机的 UWP MVVM 数据绑定(bind)(来自字符串的 textbox.text)

.net - "bad"在字典中使用对象作为键吗?

macos - 在 VSCode 中,如何将我的默认终端更改为 iterm?

shell - 如何在shell内运行fish文件

c# - 关闭的 XML 根据单元格值更改整行背景颜色

c# - 如何通过代码将复选框添加到滚动查看器?

.net - Windows 安装程序出错 ... "Unable to get installer types"

c# - FluentValidation 涉及正在验证的对象中的 2 个属性

compilation - 使用 Compass 编译多个样式表,不同的输出样式和 ".min"文件名