Monitoring settings in a configsection of your app.config for changes

The usage:

public static void Main()
{
    using(var configSectionAdapter = new ConfigurationSectionAdapter<ACISSInstanceConfigSection>("MyConfigSectionName"))
    {
        configSectionAdapter.ConfigSectionChanged += () =>
        {
            Console.WriteLine("File has changed!  New setting is " + configSectionAdapter.ConfigSection.MyConfigSetting);
        };
        Console.WriteLine("The initial setting is " + configSectionAdapter.ConfigSection.MyConfigSetting);
        Console.ReadLine();
    }
}
 

The meat:

public class ConfigurationSectionAdapter<T> : IDisposable
    where T : ConfigurationSection
{
    private readonly string _configSectionName;
    private FileSystemWatcher _fileWatcher;

    public ConfigurationSectionAdapter(string configSectionName)
    {
        _configSectionName = configSectionName;
        StartFileWatcher();
    }

    private void StartFileWatcher()
    {
        var configurationFileDirectory = new FileInfo(Configuration.FilePath).Directory;
        _fileWatcher = new FileSystemWatcher(configurationFileDirectory.FullName);
        _fileWatcher.Changed += FileWatcherOnChanged; 
        _fileWatcher.EnableRaisingEvents = true;
    }

    private void FileWatcherOnChanged(object sender, FileSystemEventArgs args)
    {
        var changedFileIsConfigurationFile = string.Equals(args.FullPath, Configuration.FilePath, StringComparison.OrdinalIgnoreCase);
        if (!changedFileIsConfigurationFile)
            return;

        ClearCache();
        OnConfigSectionChanged();
    }

    private void ClearCache()
    {
        ConfigurationManager.RefreshSection(_configSectionName);
    }

    public T ConfigSection
    {
        get { return (T)Configuration.GetSection(_configSectionName); }
    }

    private System.Configuration.Configuration Configuration
    {
        get { return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); }
    }

    public delegate void ConfigChangedHandler();
    public event ConfigChangedHandler ConfigSectionChanged;

    protected void OnConfigSectionChanged()
    {
        if (ConfigSectionChanged != null)
            ConfigSectionChanged();
    }

    public void Dispose()
    {
        _fileWatcher.Changed -= FileWatcherOnChanged; 
        _fileWatcher.EnableRaisingEvents = false;
        _fileWatcher.Dispose();
    }
}

1 Comment

  • It's actually for a windows service/console app, which is why I said "app.config". In an asp.net web app, I like the solution you linked if it's for a file that is NOT the web.config.. As you know (but just for reference of anyone coming upon this post), if your setting IS in your web.config, any change to that file causes the application to recycle and subsequently refresh your settings "automatically".

Comments have been disabled for this content.