如何更新app.config文件的价值? [英] How to update an value in app.config file?

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

问题描述

这是我的代码:

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = configuration.AppSettings.Settings;  
settings["IP"].Value = "10.0.0.2";
configuration.Save(ConfigurationSaveMode.Modified);

当我在设置[IP]。值突破行,我得到正确的值。结果
的方法完成,没有任何错误,但app.config文件保持不变。

when I break on settings["IP"].Value line, i get the correct value.
The method completes without any errors but app.config file remains unchanged.

推荐答案

此代码应工作:

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

config.AppSettings.Settings["IP"].Value = "10.0.0.2";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

这篇关于如何更新app.config文件的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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