Apr 10, 2012 03:31 AM|Steven Cheng - MSFT|LINK
As for ASP.NET web application (or WCF service web application), we use web.config file to store those application and runtime specific settings. And whenever the web application starts, it will load the configuration file(web.config) settings into memory
and cache it(this is also the same for desktop .NET applications like winform or WPF apps). Therefore, change app.config/web.config file at runtime is not quite recommended. And for web application, if you programmtically change it at runtime, the application(appdomain)
will be restarted (is this your expected behavior)?
For web.config file manipulation, you can use the Webconfigurationmanager class(see web references below):
#How to: Access ASP.NET Configuration Settings Programmatically
#Programmatically manipulating web.config in ASP.NET 2.0 and ASP.NET 3.5
In addition, if such kind of modification is only done at deployment time(when deploy the web app to different servers), you can consider using the "configSource" attribute to split your web.config (actually those certain sections in web.config file) into
separate xml files. Thus, you only need to provide a new version of the separate xml file when deploying the application to a new server(where new configuration values like connectionString is required).
#Using configSource to split configuration files
#Manage Web.config from an XML file by using the configSource attribute in ASP.NET 2.0