public void UpdateConfigFile(string key, string value)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, value);
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection("appSettings");
}
This code snippet updates a value in the config (by removing it and then adding it) and then refreshes the section that contains the value (in this case the assSettings section).