我想修改 app.config 中 appSetting 部分的值。所以我写道,
Console.WriteLine(ConfigurationManager.AppSettings["name"]);
Console.Read();
Configuration config=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["name"].Value = "raja";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine(ConfigurationManager.AppSettings["name"]);
Console.Read();
执行上述代码后,我验证了 app.config 中“name”元素的值是否已更改。但没有变化。
我的代码有什么问题吗?或者还有其他方法可以做到这一点吗?
这段代码对我有用:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["test"].Value = "blah";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
注意:如果您使用 F5 运行它,它不会更新解决方案项“app.config”,而是更新 bin/ 文件夹中的“.exe.config”项。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)