January 4, 2017 08:20 by
Peter
In this post, I will tell you about How To Change app.config Data in ASP.NET. Please write the following code 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];
}
App.config file
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.
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");
}