Modify App.Config Programmatically

That is pretty much alike to modifying web.config, see previous post here.

Code snippet as follow which open up the app.config, modify values, save it back.

Configuration l_appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appSettings = (AppSettingsSection)l_appConfig.GetSection("appSettings");
//let say you want to modify the setting name DatabaseHost
appSettings.Settings["DatabaseHost"].Value = "blahblahblah";
l_appConfig.Save();
ConfigurationManager.RefreshSection("appSettings");

After you run the program and execute this code. You will find that the app.config won’t change.
Because the .config which get modified it in \bin\Debug or \bin\Release depends on what mode you are running.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s