Refresh AppSettings keys

The .Net runtime caches the app.config file, therefore, when the app.config file is updated, you often need to restart the application in order for the changes to show up. Sure, you can get the keys by calling ConfigurationManager.OpenExeConfiguration() and you would be getting the latest version of the config file every time, but it's probably not the most efficient way to do it.

Here is how I like to do it. I call ConfigurationManager.AppSettings["MyAppSetting"] to get my key, which gets the cached version of the key so it's very fast. And I use a file system watcher to watch for changes in the config file. Whenever the config file is updated, I call ConfigurationManager.RefreshSection("AppSettings") to refresh the app settings keys. This way, the application would not have to be restarted whenever the config file changes.

Here is what the class might look like.

using System;
using System.Configuration;
using System.IO;
 
namespace myApp
{
    /// <summary>
    /// This class will refresh the cached version of the config file when it is changed.
    /// </summary>
    public static class ConfigurationWatcher
    {  
        private static FileSystemWatcher _fileWatcher;   
 
        internal static void Start()
        {
            if (_fileWatcher == null)
            { 
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                string path = Path.GetDirectoryName(config.FilePath);
                string filename = Path.GetFileName(config.FilePath);                   
 
                _fileWatcher = new FileSystemWatcher();
                _fileWatcher.Path = path;
                _fileWatcher.Filter = filename;
                _fileWatcher.NotifyFilter = (NotifyFilters.CreationTime | NotifyFilters.LastWrite |
                                NotifyFilters.FileName);
                _fileWatcher.Changed += OnChange;
                _fileWatcher.EnableRaisingEvents = true;
            }           
        }
 
        private static void OnChange(object source, FileSystemEventArgs e)
        {
            ConfigurationManager.RefreshSection("AppSettings");
        }
    }
}
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Saturday, March 07, 2009 5:11 PM
Print

Comments on this entry:

# re: Refresh AppSettings keys

Left by Japan at 8/10/2009 8:14 AM
Gravatar
Yes thanks, Its working !!!

# re: Refresh AppSettings keys

Left by André Monteiro at 1/19/2010 12:46 PM
Gravatar
Excellent article!

# A small typo

Left by yallie at 4/2/2010 1:41 PM
Gravatar
Thanks for the example!
I just want to mention that there should be ConfigurationManager.RefreshSection("appSettings") instead of "AppSettings" (with capital "A").

# re: Refresh AppSettings keys

Left by Jim at 6/17/2010 3:59 AM
Gravatar
I found this solution while searching for a resolution to the cached appSettings. Restarting IIS isn't always an option for use because there are a number of applications on the same server. Thanks, it was a huge help!

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910