Search code examples
c#.netfilesystemwatcher

FileSystemWatcher detects continuous change


I'm using a FileSystemWatcher to detect changes in a file - I monitor the directory and then see if the file passed in through the event args matches the file I am interested in.

We have a situation with one client where the changed event is being constantly fired, yet when I check the modified date nothing has changed.

This is causing all kinds of issues for us. I can (and I am) check whether the last modified date has changed before acting on the event.

I can't easily debug it because I can't reproduce it, although I can put a debug build on a client machine - although sometimes it stops happening then.

But I would like to know if there is a known cause of this issue which I can prevent.

[Edit] I didn't originally add this because I'm pretty sure its not relevant, but a previous version of the code was making a lot of changes to the file in question, more than we could deal with in code. However the current version doesn't have that issue and the machines have been reset since then, so I can't imagine its causing the current issue.


Solution

  • It is possible that other change events are occurring, such as file attributes or security ACL modifications. Take a look at the notify filter:

    https://msdn.microsoft.com/en-us/library/system.io.notifyfilters(v=vs.110).aspx

    You can set this to filter to include only the changes you want. In this case, it sounds like you want simply the write time modifications. You can assign it to the NotifyFilter property on the watcher.

    It is tough to say what is occurring in your exact situation, but it is possible that the file attributes are being altered (by an indexer, for example) or some such background process. These modifications may not result in a change to the last write time, but still may cause the watcher to fire.