In VB
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration( _ ConfigurationUserLevel.None) config.ConnectionStrings.ConnectionStrings("MyDB") _ .ConnectionString = "A_DB_CONNECTION_STRING" config.AppSettings.Settings("MySetting").Value = "A_SETTING_VALUE" config.Save(ConfigurationSaveMode.Modified) ConfigurationManager.RefreshSection("connectionStrings") ConfigurationManager.RefreshSection("appStrings")
In C#
Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); config.ConnectionStrings.ConnectionStrings["MyDB"] .ConnectionString = "A_DB_CONNECTION_STRING"; config.AppSettings.Settings["MySetting"].Value = "A_SETTING_VALUE"; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("connectionStrings"); ConfigurationManager.RefreshSection("appStrings");
In these examples, I am changing a connection string value and an app setting value. Once the config has been saved the modified section(s) need to be refreshed to ensure the new mods are loaded back into memory.
Remember that if your app is re-installed, then the modified app.config will be overwritten.