C#: Saving Application Settings During Runtime
Normally when you would try to save the application settings during runtime you get an error that the application settings are read only. This is a way that I found around that.
Essentially, you have to remove and re-add the settings.
I should also note that this only works in Visual Studio 2005 as I found out last night when i tried it in 2008.
// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration
(ConfigurationUserLevel.None);
//SAVE ALL OF THE SETTINGS
config.AppSettings.Settings.Remove("sqlServer");
config.AppSettings.Settings.Add("sqlServer", this.txbServer.Text);
config.AppSettings.Settings.Remove("sqlDatabase");
config.AppSettings.Settings.Add("sqlDatabase", this.txbDatabase.Text);
config.AppSettings.Settings.Remove("sqlUserName");
config.AppSettings.Settings.Add("sqlUserName", this.txbUserName.Text);
config.AppSettings.Settings.Remove("sqlUserPassword");
config.AppSettings.Settings.Add("sqlUserPassword", this.txbPassword.Text);
config.AppSettings.Settings.Remove("sqlSecurity");
config.AppSettings.Settings.Add("sqlSecurity", this.txbSecurity.Text);
config.AppSettings.Settings.Remove("extListCollection");
config.AppSettings.Settings.Add("extListCollection", this.txbExCC.Text);
config.AppSettings.Settings.Remove("sumListCollection");
config.AppSettings.Settings.Add("sumListCollection", this.txbSumCC.Text);
config.AppSettings.Settings.Remove("indListCollection");
config.AppSettings.Settings.Add("indListCollection", this.txbIndAliasCC.Text);
// Save the config file.
config.Save(ConfigurationSaveMode.Full);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
