We Can modify the configuration files app settings and the Connection Strings through Configuration Manager.
- Add the Reference of System.Configuration for accessing the ConfigurationManager Class.
The Bellow Method is used to Modify the Value of the AppSettings Key Values.
private void ModifyKeyValue(string strKey, string strValue)
{
// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Check if the key available
if (!Array.Exists(config.AppSettings.Settings.AllKeys, delegate(string s) { return (s == strKey); }))
{
//Create the Key and assign value
config.AppSettings.Settings.Add(strKey, strValue);
}
else
{
// Add an Application Setting.
config.AppSettings.Settings[strKey].Value = strValue;
}
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
}
We can also Remove the already available Keys and Values after that recreate the New Keys using the bellow method:
private void DropAndCreateKey()
{
// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Remove("Name");
config.AppSettings.Settings.Add("Name", "Krishna");
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
String siteID = ConfigurationManager.AppSettings["Name"].ToString(); //GetValue("Name");
}



Read User's Comments