Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AppSettings中的更改需要重新启动我的应用程序如何避免?_C#_.net_Configuration_Appsettings_Configurationmanager - Fatal编程技术网

C# AppSettings中的更改需要重新启动我的应用程序如何避免?

C# AppSettings中的更改需要重新启动我的应用程序如何避免?,c#,.net,configuration,appsettings,configurationmanager,C#,.net,Configuration,Appsettings,Configurationmanager,我正在使用C#.NET 2.0 Windows应用程序 我正在使用app.config进行应用程序设置 但AppSettings中的更改并不反映运行时,它需要重新启动应用程序 我怎样才能避免呢 这是我用来读写应用程序设置的代码片段 我在读这样的背景 string temp = ConfigurationManager.AppSettings.Get(key); 我正在像这样更新值,其中node是当前配置/appSettings节点 node.Attributes["value"].Value

我正在使用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);
您可以尝试调用从磁盘刷新文件的AppSettings部分。刷新后,您应该能够读取新值

ConfigurationManager.RefreshSection("appSettings")

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

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

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

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

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

ConfigurationManager.RefreshSection("appSettings");
工作


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

是,这也可能,但这可能不是最有效的方法。这里您也使用ConfigurationManager。有趣的是,这似乎不会刷新通过ConfigurationManager.AppSettings[“mySetting”]检索到的值。