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

Posted by dotjosh on ASP.net Weblogs See other posts from ASP.net Weblogs or by dotjosh
Published on Wed, 24 Mar 2010 15:08:00 GMT Indexed on 2010/03/24 19:33 UTC
Read the original article Hit count: 310

Filed under:
|

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();
    }
}

© ASP.net Weblogs or respective owner

Related posts about .NET

Related posts about c#