AppSettings 的更改需要重新启动我的应用程序,我该如何避免?
我正在使用 C# .NET 2.0 Windows 应用程序。
我正在使用 app.config 作为我的应用程序设置。
但 AppSettings 中的更改不会反映运行时,它需要重新启动应用程序。
我怎样才能避免呢。
这是我用来读取和写入应用程序设置的代码片段。
我正在阅读这样的设置
string temp = ConfigurationManager.AppSettings.Get(key);
我正在更新这样的值,其中节点是当前配置/appSettings 节点
node.Attributes["value"].Value = value;
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
I'm using C# .NET 2.0 Windows Application.
and I'm using app.config for my Application Settings.
but change in AppSettings doesn't reflected runtime, it Needs Application to be restarted.
How can I avoid it.
Here is my code snippet I used to read and write the Application Settings.
I'm reading the Setting like this
string temp = ConfigurationManager.AppSettings.Get(key);
I'm updating the value like this where node is the current configuration/appSettings Node
node.Attributes["value"].Value = value;
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以尝试打电话
to refresh the AppSettings section of the file from disk. Once they have been refreshed, you should be able to read the new values.
我刚刚测试过这个,它确实有效。
You could try calling
to refresh the AppSettings section of the file from disk. Once they have been refreshed, you should be able to read the new values.
I've just tested this and it does indeed work.
或者,您可以创建一个单例“选项”来保留您的应用程序设置并为您执行读/写操作。 加载后,更改 .config 不需要重新加载,您只需在单例上设置一个属性并调用 .Save() 方法即可。
设置的“运行时”版本位于单例中,无需从磁盘读取。
Alternatively, you could create a singleton 'Options' to hold on to your application settings and perform your read/writes for you. Once loaded, changing the .config doesn't require reloading, you simply set a property on the singleton and call your .Save() method.
The 'runtime' version of your settings is in the singleton, no need to read from disk.
不要使用 ConfigurationManager 读取设置,而是使用:
Dont use ConfigurationManager to read settings, instead use:
作品!!
但要小心,如果我们处于调试模式,配置文件可以被称为xxxxx.vshost.exe.config,其中xxxxx是你的项目名。
works!!
But be careful that if we are in debug mode, the configuration file can be called xxxxx.vshost.exe.config, where xxxxx is your project name.