Save and reload app.config (applicationSettings) at runtime

I saved my application configuration in app.config, Visual Studio. I created some application key in the settings tab of the project properties dialog, then I installed this key at the application level (NOT at the user level).

Visual Studio automatically creates 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 run time, here is my code that allows me to save the DatabasePath value 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 are not loaded by the application until I restart the application; is there any way to reload configuration settings at runtime?

I also want to replace the fixed value of the applicationSettings section name (AleTest.Properties.Settings) with the variable value, is there a variable in the structure, suppose this value (AleTest.Properties.Settings)?

+8
c # configuration
source share
4 answers

You need to make a call to ConfigurationManager.RefreshSection to read the values ​​from disk.

+5
source share

I did some tests, and here is the result.

For the automatically created Settings class, call ConfigurationManager.RefreshSection("applicationSettings"); does not apply the changed values ​​to elements labeled ApplicationScopedSettingAttribute , it applies the changes to future calls through the ConfigurationManager members (and is not sure about UserScopedSettingAttribute ).

Instead, call Settings.Default.Reload();

+4
source share

What you want is achieved by creating a custom ConfigSection that allows you more control and allows you to change the name. The configuration manager has an update section that allows you to reload data.

+3
source share

Aleroot code for updating values ​​worked fine. Although Properties.Settings is written only (not set).

To update, this worked for me: ConfigurationManager.RefreshSection ("applicationSettings");

But I use this to access the parameter: string test = Properties.Settings.Default.MyString; MessageBox.Show ("Parameter / Settings MyString =" + test);

0
source share

All Articles