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 DotNetKicks.com  

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.

Quotations

"Success is the ability to go from one failure to another with no loss of enthusiasm."
Sir Winston Churchill

Donate with PayPal - it

Calendar

<<  September 2014  >>
MoTuWeThFrSaSu
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

View posts in large calendar

Disclaimer

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

© Copyright 2005 - 2014

Search