Search code examples
c#servicefilesystemwatcher

C# FileSystemWatcher not triggering correctly in Service


I'm working on a service where in my OnStartmethod I have the following lines of code to set up my FileSystemWatcher

Log.Info($"File location {_location}");
var watcher = new FileSystemWatcher(_location);      
watcher.Changed += new FileSystemEventHandler(OnChanged);

Then in my OnChanged method I am wanting to start a timer like so:

private void OnChanged(object source, FileSystemEventArgs e)
{
    Log.Info($"A file has been placed in {_location} starting timer");
    OnTimer(null, null); //run immediately at startup
    StartEventTimer();
}

The timer code works, so I know that isn't an issue, likewise in my log I know it is checking for the correct location. What is it that I'm missing?

All I'm wanting my code to do is to trigger my timer, the moment a file is placed in my target location yet I've not been able to do so. Am I correct in that I should be using FileSystemWatcherto do this, or should I use something else as this code is within a service?


Solution

  • There are a couple of things it could be based on what you said there.

    The first thing of note is that the declaration for var watcher looks like it's not a class variable and will go out of scope when it exits OnStart(). You'll need to move the declaration outside of that.

    The second item of interest is that it looks like EnableRaisingEvents isn't being set. A working example of the FileSystemWatcher is below.

    public class SomeService
    {
        private FileSystemWatcher _watcher;
        public void OnStart()
        {
            // set up the watcher
            _watcher = new FileSystemWatcher(_location);
            _watcher.Path = path;
            _watcher.NotifyFilter = NotifyFilters.LastWrite;
            _watcher.Filter = "*.*";
            _watcher.Changed += new FileSystemEventHandler(OnChanged);
            _watcher.EnableRaisingEvents = true;
        }
    }
    

    EDIT

    As Ben Hall mentioned, it is possible that multiple events can be raised for the same file when a file is moved into the folder. As per the MSDN documentation;

    Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher