partial class DaveKehring : IProgrammer { }  RSS 2.0
# Wednesday, April 01, 2009

I was running into a problem when trying to modify the app.config file for an application at runtime. Specifically, I was adding connection strings to the <connectionStrings> section but further along in my code the changes were not visible. However, the configuration file was changed! How could this be? After much head scratching and research, I find the problem.

To change the app.config file I was opening the config file like this:

     System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

I then add or update individual connection strings as need (that code's not important). Finally, I save the changes:

     config.Save(System.Configuration.ConfigurationSaveMode.Modified);

Further on in the code I then attempt to access the connection strings, but instead I use the ConfigurationManager class like this:

     ConnectionStringSettings dbConn = System.Configuration.ConfigurationManager.ConnectionStrings[DefaultDatabaseKey];

The problem is that the ConfigurationManager - which is a static class - already has a stored copy of the configuration file in its internals. The answer is to call RefreshSection("...") after saving changes to the config file:

   config.Save(System.Configuration.ConfigurationSaveMode.Modified);
   System.Configuration.
ConfigurationManager.RefreshSection("connectionStrings");

 

Wednesday, April 01, 2009 11:21:45 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Configuration
Navigation
Archive
<April 2009>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
About the author/Disclaimer

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

© Copyright 2012
Dave Kehring
Sign In
Statistics
Total Posts: 11
This Year: 0
This Month: 0
This Week: 0
Comments: 2
Themes
Pick a theme:
All Content © 2012, Dave Kehring
DasBlog theme 'Business' created by Christoph De Baene (delarou)