Anders
Anders

Reputation: 12716

Differentiate between Changed and Created events in FileSystemWatcher?

I'm using the FileSystemWatcher to monitor a certain directory, and I need to raise one event when someone saves edits to a file, and another when they create or move a file.

Monitoring moved files works fine using a combination of the Deleted and Created events. And when someone saves edits to a file the Changed event does indeed get raised. However, when they move a file the Changed event gets raised too, and that interferes with the handling I've got for the Created and Deleted events.

So basically, I want to raise the Changed event only when the user saves edits to a file, while not when the user moves or creates a file. I tried using the ChangeType property to check if it was in fact a Changed event or a Created event, but to my surprise, the ChangeType Changed was raised for the Change event even when moving or creating a file, not the ChangeType Created (which supposedly should be one of the types).

So I don't know how to check that the Change event is actually triggered by a file edit, rather than file creation or move...

Any ideas?

Upvotes: 3

Views: 3107

Answers (1)

Jalal Said
Jalal Said

Reputation: 16162

This is normal behavior when you apply some of NotificationFilter such as NotifyFilter. Attributes and NotifyFilter.LastAccess, it will even notify Changed twice if both filters was applied when file move "Deleted - Created - Changed - Changed", So:

  • Simply don't add not relevant notify filters; If you just remove NotifyFilter. Attributes and NotifyFilter.LastAccess you will not receive Changed event when move file, only Deleted then Created.
  • If you are wishes to stick with the notify filters that you are already using, there is other hint here to allow you to generate a dirty solution, it is the fact that when the file moved, the sequence of notification will be Deleted, Created then Changed, they will be always in this order..

Upvotes: 5

Related Questions