For a .NET 4.0 console application, none of these worked for me. So I modified Kevn Aenmey's answer as below and it worked:
private static void UpdateSetting(string key, string value)
{
Configuration configuration = ConfigurationManager.
OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save();
ConfigurationManager.RefreshSection("appSettings");
}
Only the first line is different, constructed upon the actual executing assembly.