aleroot
aleroot

Reputation: 72616

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

Upvotes: 13

Views: 30771

Answers (4)

Oleg Savelyev
Oleg Savelyev

Reputation: 968

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();

Upvotes: 7

peter.cyc
peter.cyc

Reputation: 1813

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

Upvotes: 0

rerun
rerun

Reputation: 25495

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.

Upvotes: 3

Josh
Josh

Reputation: 44906

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

Upvotes: 9

Related Questions