//下面句很重要,强制程序重新获取配置文件中appSettings节点中所有的值,否则更改后要到程序关闭后才更新,因为程序启动后默认不再重新获取.
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
//获取Configuration对象
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//根据Key读取<add>元素的Value
string name = config.AppSettings.Settings["Version"].Value;
//写入<add>元素的Value
config.AppSettings.Settings["Version"].Value = (Convert.ToInt32(version) + 1).ToString();
//增加<add>元素
config.AppSettings.Settings.Add("url", " http://www.xieyc.com ");
//一定要记得保存,写不带参数的config.Save()也可以
config.Save(ConfigurationSaveMode.Modified);
//刷新,否则程序读取的还是之前的值(可能已装入内存)
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
//获取Configuration对象
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//根据Key读取<add>元素的Value
string name = config.AppSettings.Settings["Version"].Value;
//写入<add>元素的Value
config.AppSettings.Settings["Version"].Value = (Convert.ToInt32(version) + 1).ToString();
//增加<add>元素
config.AppSettings.Settings.Add("url", " http://www.xieyc.com ");
//一定要记得保存,写不带参数的config.Save()也可以
config.Save(ConfigurationSaveMode.Modified);
//刷新,否则程序读取的还是之前的值(可能已装入内存)
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
本文介绍了如何在程序运行过程中强制更新配置文件中appSettings节点的数据,并确保应用程序能够实时获取这些更改。通过使用System.Configuration.ConfigurationManager类的方法,如RefreshSection、OpenExeConfiguration、AppSettings等,可以实现配置文件的动态更新。此外,文章还详细解释了如何获取、修改和保存配置文件中的特定设置,以及刷新配置以确保应用程序能够立即应用这些更改。
1820

被折叠的 条评论
为什么被折叠?



