在运行时保存并重新加载 app.config(applicationSettings)
我已将应用程序的配置存储在 app.config 中,通过 Visual Studio,我在项目属性对话框的设置选项卡上创建了一些应用程序密钥,然后我在应用程序级别(而不是用户级别)设置了该密钥。
Visual Studio 自动生成以下 xml 文件 (app.config):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AleTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<AleTest.Properties.Settings>
<setting name="DatabasePath" serializeAs="String">
<value>Test.s3db</value>
</setting>
<setting name="DatabaseUser" serializeAs="String">
<value />
</setting>
<setting name="DatabasePass" serializeAs="String">
<value />
</setting>
</AleTest.Properties.Settings>
</applicationSettings>
</configuration>
现在我想在运行时保存并重新加载设置,这是我的代码,允许在配置文件中保存值 DatabasePath
:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["AleTest.Properties.Settings"];
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;
//Database Configuration Setting
SettingElement applicationSetting = clientSection.Settings.Get("DatabasePath");
applicationSetting.Value.ValueXml.InnerXml = this.textBoxPath.Text.Trim();
applicationConfigSection.SectionInformation.ForceSave = true;
config.Save();
问题是使用此代码,在我重新启动应用程序之前,应用程序不会加载新设置;有没有办法在运行时重新加载配置设置?
我还想用变量值替换 applicationSettings
部分(AleTest.Properties.Settings)名称的固定值,框架中存在一个变量并假设该值(AleTest.Properties.Settings) ?
I've stored configuration of my application in the app.config, by Visual Studio I've created some application key on the settings tab of project properties dialog, then I've set this key at application level(NOT at user level).
Visual Studio automatically generate the following xml file (app.config) :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AleTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<AleTest.Properties.Settings>
<setting name="DatabasePath" serializeAs="String">
<value>Test.s3db</value>
</setting>
<setting name="DatabaseUser" serializeAs="String">
<value />
</setting>
<setting name="DatabasePass" serializeAs="String">
<value />
</setting>
</AleTest.Properties.Settings>
</applicationSettings>
</configuration>
Now I want to save and reload the settings at runtime, here's my code that allow to save the value DatabasePath
in the configuration file:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["AleTest.Properties.Settings"];
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;
//Database Configuration Setting
SettingElement applicationSetting = clientSection.Settings.Get("DatabasePath");
applicationSetting.Value.ValueXml.InnerXml = this.textBoxPath.Text.Trim();
applicationConfigSection.SectionInformation.ForceSave = true;
config.Save();
The problem is that with this code the new settings aren't loaded by application until I restart the application; is there a way to reload the config settings at runtime?
I also want to replace the fixed value of the name of applicationSettings
section (AleTest.Properties.Settings) with a variable value, exist a variable in the framework the assume this value (AleTest.Properties.Settings) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要调用 ConfigurationManager.RefreshSection 以便从磁盘重新读取值。
You need to make a call to ConfigurationManager.RefreshSection in order to have the values re-read from disk.
我做了一些测试,这是结果。
对于自动生成的类
Settings
,调用ConfigurationManager.RefreshSection("applicationSettings");
不会对用ApplicationScopedSettingAttribute
标记的成员应用修改后的值,它通过ConfigurationManager
成员将更改应用于将来的调用(并且不确定UserScopedSettingAttribute
)。而是调用
Settings.Default.Reload();
I did a some tests and here is result.
For auto generated class
Settings
the call ofConfigurationManager.RefreshSection("applicationSettings");
doesn't apply the modified values for members marked withApplicationScopedSettingAttribute
, it applies the changes to future calls viaConfigurationManager
members (and not sure aboutUserScopedSettingAttribute
).Instead call
Settings.Default.Reload();
您想要的可以通过创建自定义 ConfigSection 来完成,它允许您更多控制并允许您更改名称。配置管理器有一个刷新部分,允许您重新加载数据。
What you want is accomplish able by creating an custom ConfigSection which allows you more control and allows you to change the name. Configuration manager has a refresh section which will allow you reload the data.
Aleroot 用于更新值的代码运行良好。
尽管 Properties.Settings 是只写的(没有设置)。
更新一下,这对我有用:
ConfigurationManager.RefreshSection(“applicationSettings”);
但我用它来访问参数:
字符串测试 = Properties.Settings.Default.MyString;
MessageBox.Show("Paramètres/Settings MyString = " + test);
Aleroot's code for updating values worked fine.
In spite of Properties.Settings being write only (no set).
To refresh, this worked for me:
ConfigurationManager.RefreshSection("applicationSettings");
But I'm using this to access the parameter:
string test = Properties.Settings.Default.MyString;
MessageBox.Show("Paramètres/Settings MyString = " + test);