This code works, but it destroys comments and formatting in Web.config. Is there a way to write to Web.config that won't destroy formatting?
var webConfig = WebConfigurationManager.OpenWebConfiguration("~");
var settings = webConfig.AppSettings.Settings;
settings["Foo"].Value = "Bar";
webConfig.Save();