0

I have a method in my service where I'm watching the services config file. It looks like this:

private void WatchConfigurationFile()
{
    var fileLocation = Assembly.GetExecutingAssembly().Location;


    watcher.Path = Path.GetDirectoryName(fileLocation);
    watcher.NotifyFilter = NotifyFilters.LastWrite;
    watcher.Filter = fileLocation + ".config";

    watcher.Changed += new FileSystemEventHandler(OnConfigChange);
    watcher.EnableRaisingEvents = true;


    Log.DebugFormat("config location: {0}", watcher.Filter.ToString());

}

The log returns the complete file path location of my targeted file, including the location of where it is stored: C/Users etc etc

Yet, when I update my config file, the changes aren't displayed in my log files. The logging is done like this:

private void OnConfigChange(object source, FileSystemEventArgs e)
{
    ConfigurationManager.RefreshSection("appSettings");
    Log.DebugFormat("Updated ConnectionString: {0}", ConfigurationManager.AppSettings["dbConn"]);
}  

However in my file watcher class, if I change the filter to the actual name of the file, which in my case is the service exe name with a '.config' at the end. It works fine.

Why is it one works and the other doesn't?

N0xus
  • 2,674
  • 12
  • 65
  • 126

1 Answers1

2

You should use watcher.Filter = "*.config"; for all config files or else watcher.Filter = "yourFileName.config"; for a specific file. No need to specify the full path here;

As per MSDN: To watch changes in all files, set the Filter property to an empty string (""). To watch a specific file, set the Filter property to the file name. For example, to watch for changes in the file MyDoc.txt, set the Filter property to "MyDoc.txt". You can also watch for changes in a certain type of file. For example, to watch for changes in any text files, set the Filter property to ".txt". Use of multiple filters such as ".txt|*.doc" is not supported.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88