ASP.Net dynamic update of web.config without loosing comments

Share on Facebook

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.

kick it on  

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListkick it on DotNetKicks.comTwitThis

Comments are closed

About Me

When not scratching my head for solutions to software challenges, I spend my time playing with my little boy - Michael Jnr.


"Every day I remind myself that my inner and outer life are based on the labors of other men, living and dead, and that I must exert myself in order to give in the same measure as I have received and am still receiving"
Albert Einstein

Donate with PayPal - it


<<  October 2014  >>

View posts in large calendar


The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2005 - 2014