private void SaveAppSetting(string key, string value) { Configuration manager = OpenConfigFile(); if (manager != null) { // NOTE: Most likey scenario in a production environment if (!string.IsNullOrEmpty(manager.AppSettings.Settings[key].Value)) manager.AppSettings.Settings.Remove(key); manager.AppSettings.Settings.Add(key, value); manager.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } else { // NOTE: Alternate scenario when settings cannot be saved through a ConfigurationManager, particulary when running under the Visual Studio Web Development Server (as opposed to IIS or IIS Express). ConfigurationEditor editor = new ConfigurationEditor(Path.Combine(Server.MapPath(LocalWebConfig), "Web.config")); if (editor.KeyExists(key)) editor.UpdateKey(key, value); else editor.AddKey(key, value); editor.SaveDoc(); } }