Changing App.config at Runtime

I understand this is quite an old thread, but I could not get the listed methods to work. Here is a simpler version of the UpdateAppSettings method (using .NET 4.0): private void UpdateAppSettings(string theKey, string theValue) { Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (ConfigurationManager.AppSettings.AllKeys.Contains(theKey)) { configuration.AppSettings.Settings[theKey].Value = theValue; } configuration.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(“appSettings”); } Pretty readable and avoids … Read more

How to get the values of a ConfigurationSection of type NameValueSectionHandler

Suffered from exact issue. Problem was because of NameValueSectionHandler in .config file. You should use AppSettingsSection instead: <configuration> <configSections> <section name=”DEV” type=”System.Configuration.AppSettingsSection” /> <section name=”TEST” type=”System.Configuration.AppSettingsSection” /> </configSections> <TEST> <add key=”key” value=”value1″ /> </TEST> <DEV> <add key=”key” value=”value2″ /> </DEV> </configuration> then in C# code: AppSettingsSection section = (AppSettingsSection)ConfigurationManager.GetSection(“TEST”); btw NameValueSectionHandler is not supported any … Read more

ConfigurationManager.AppSettings – How to modify and save?

I know I’m late 🙂 But this how i do it: public static void AddOrUpdateAppSettings(string key, string value) { try { var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var settings = configFile.AppSettings.Settings; if (settings[key] == null) { settings.Add(key, value); } else { settings[key].Value = value; } configFile.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); } catch (ConfigurationErrorsException) { Console.WriteLine(“Error writing app settings”); } } … Read more

What’s the difference between the WebConfigurationManager and the ConfigurationManager?

WebConfigurationManger knows how to deal with configuration inheritance within a web application. As you know, there could be several web.config files in one applicaion – one in the root of the site and any number in subdirectories. You can pass path to the GetSection() method to get possible overridden config. If we’d looke at WebConfigurationManager … Read more