Monitoring settings in a configsection of your app.config for changes
- by dotjosh
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();
}
}