13

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
aleroot
  • 71,077
  • 30
  • 176
  • 213
  • I had a different problem: Writing into the 'applicationSettings' section of the web.config by using the Configuration object. Thx for showing me how to do that. :) – Kai Hartmann Jul 01 '13 at 11:20

4 Answers4

8

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

Josh
  • 44,706
  • 7
  • 102
  • 124
  • 1
    I've tried with ConfigurationManager.RefreshSection("applicationSettings"); but don't work... Have you tried ? – aleroot Jun 13 '11 at 20:51
  • I've used this several times on different projects. I'm not sure what specific problems you might be running into here, but I will take a look later to see if I can repro. – Josh Jun 13 '11 at 21:01
  • See my answer. The Settings.Default.Reload(); works in my case. – Oleg Savelyev Aug 25 '15 at 19:25
  • Hi, as explained in http://stackoverflow.com/a/4950694/481812 - I had to use the ResetConfigManager hack in combination with RefreshSection("appSettings") – Robert Cutajar May 11 '16 at 11:43
7

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

Oleg Savelyev
  • 968
  • 2
  • 8
  • 12
3

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.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
rerun
  • 25,014
  • 6
  • 48
  • 78
0

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

peter.cyc
  • 1,763
  • 1
  • 12
  • 19