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)?
aleroot
source share