The code given below is used to change app.config data at runtime.
- private static void SetSetting(string key, string value)
- {
- Configuration configuration =
- ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- configuration.AppSettings.Settings[key].Value = value;
- configuration.Save(ConfigurationSaveMode.Full, true);
- ConfigurationManager.RefreshSection("appSettings");
- }
The code given below is used to fetch app.config data.
- private static string GetSetting(string key)
- {
- return ConfigurationManager.AppSettings[key];
- }

Key is lang and value is English.
Output
We get the "lang" value as English, this value is fetched from App.config file.

Modify App.config value at runtime.
We get the "lang" value as English, this value is fetched from App.config file.

Modify App.config value at runtime.

Code
- public Form1()
- {
- InitializeComponent();
- string objAppConfigValue = GetSetting("lang");
- SetSetting("lang", "Tamil");
- string objModifiedAppConfigValue = GetSetting("lang");
- }
- private static string GetSetting(string key)
- {
- return ConfigurationManager.AppSettings[key];
- }
- private static void SetSetting(string key, string value)
- {
- Configuration configuration =
- ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- configuration.AppSettings.Settings[key].Value = value;
- configuration.Save(ConfigurationSaveMode.Full, true);
- ConfigurationManager.RefreshSection("appSettings");
- }

Join the conversation! Your thoughts help the community grow.