Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save and reload app.config(applicationSettings) at runtime

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) ?

like image 706
aleroot Avatar asked Jun 13 '11 20:06

aleroot


2 Answers

You need to make a call to ConfigurationManager.RefreshSection in order to have the values re-read from disk.

like image 115
Josh Avatar answered Oct 22 '22 07:10

Josh


I did a some tests and here is result.

For auto generated class Settings the call of ConfigurationManager.RefreshSection("applicationSettings"); doesn't apply the modified values for members marked with ApplicationScopedSettingAttribute, it applies the changes to future calls via ConfigurationManager members (and not sure about UserScopedSettingAttribute).

Instead call Settings.Default.Reload();

like image 7
Oleg Savelyev Avatar answered Oct 22 '22 08:10

Oleg Savelyev