AppSettings的变化需要重新启动我的应用程序如何避免?

我正在使用C#.NET 2.0 Windows应用程序。

我正在使用app.config进行应用程序设置。

但AppSettings中的更改并未反映运行时,需要重新启动应用程序。

我怎么能避免它。

这是我用来读取和编写应用程序设置的代码片段。

我正在读这样的设置

string temp = ConfigurationManager.AppSettings.Get(key); 

我正在更新这样的值,其中node是当前配置/ appSettings节点

 node.Attributes["value"].Value = value; xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 

你可以尝试打电话

 ConfigurationManager.RefreshSection("appSettings") 

从磁盘刷新文件的AppSettings部分。 刷新后,您应该能够读取新值。

我刚刚测试了它,确实有效。

或者,您可以创建单个“选项”以保留应用程序设置并为您执行读/写操作。 加载后,更改.config不需要重新加载,只需在单例上设置属性并调用.Save()方法。

设置的“运行时”版本是单例,无需从磁盘读取。

不要使用ConfigurationManager来读取设置,而是使用:

  System.Configuration.ConfigurationManager.OpenExeConfiguration(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile).AppSettings.Settings["value"]; 
 ConfigurationManager.RefreshSection("appSettings"); 

作品!!

但要小心,如果我们处于调试模式,配置文件可以被称为xxxxx.vshost.exe.config ,其中xxxxx是您的项目名称。