Share on Facebook
- Posted by miketeye on September 28, 2007
I decided to write this short post because I did not "easily" find info in google when I needed some help on the above topic.
What I wanted to do was provide a means of updating web.config sections dynamically. I was able to do this, but as a consequence, lost all my comments in web.config. Now, you and I know the importance of comments in web.config especially when it starts to get big and ugly, so this was not my preferred solution. In order to overcome this situation, I swapped out sections of web.config I intended to update at run-time into separate external configuration files using the configSource attribute.
A careful study of the Configuration.Save() method of the System.Configuration.Configuration object which is a wrapper for a configuration file however revealed two overload methods. The first overload accepts one parameter and is the one that solved my problem. The parameter is an enumeration of type: ConfigurationSaveMode and has three values:
ConfigurationSaveMode.Full - Save everything
ConfigurationSaveMode.Minimal - Save only necessary changes
ConfigurationSaveMode.Modified - Save only modified values.
The third option to my surprised preserved all formatting in the configuration file and did not change the formatting of the file.
So my problem was solved by using the override method:
config.Save(ConfigurationSaveMode.Modified) where config is my configuration file wrapper object of type
System.Configuration.Configuration declared as follows:
Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
That's it. Hope it helps someone.