Save and reload app.config(applicationSettings) at runtime
Asked Answered
M

4

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

Mucoprotein answered 13/6, 2011 at 20:30 Comment(1)
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. :)Saintsimonianism
E
9

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

Eulalie answered 13/6, 2011 at 20:36 Comment(4)
I've tried with ConfigurationManager.RefreshSection("applicationSettings"); but don't work... Have you tried ?Mucoprotein
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.Eulalie
See my answer. The Settings.Default.Reload(); works in my case.Shorter
Hi, as explained in https://mcmap.net/q/435017/-relocating-app-config-file-to-a-custom-path - I had to use the ResetConfigManager hack in combination with RefreshSection("appSettings")Precondemn
S
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();

Shorter answered 25/8, 2015 at 19:22 Comment(1)
Settings.Default.Reload(); does not work as expected, issue persistsRomo
B
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.

Bilestone answered 13/6, 2011 at 20:37 Comment(0)
D
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);

Dennard answered 11/7, 2014 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.