Click here to Skip to main content
13,347,480 members (54,299 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

2.5K views
6 bookmarked
Posted 7 Dec 2017

NotifyFilters Enumeration Explained (FileSystemWatcher)

, 7 Dec 2017
Rate this:
Please Sign up or sign in to vote.
NotifyFilters Enumeration explained (FileSystemWatcher)

The Problem

When I first worked with the FileSystemWatcher class, I ended up experimenting with combinations of NotifyFilters and event handlers to get the desired result; it is not immediately clear, which changes to files and folders, trigger which events.

The job can only get harder when also up against known issues (just search on stackoverflow.com) with events firing twice.

EDIT: See my separate blog post, on a more reliable solution to the ‘two FileSystemWatcher events firing twice problem’.

Here, I hope to provide simple guidance on using the NotifyFilter enumeration for those just starting out with FileSystemWatcher.

What is a NotifyFilter?

These filters determine what you are watching and thus, which events can be triggered.

They can also be helpful in limiting the number of events trigger in some scenarios where complex file operations, or applications like antivirus software cause additional events to be triggered (see above) although you can’t have 100% confidence without some additional defensive coding.

Note that the default values for the NotifyFilter property are LastWrite, FileName and DirectoryName.

So, What Filters Can Result in a Changed Event Being Triggered?

  • Attributes
  • CreationTime
  • LastAccess
  • LastWrite
  • Security
  • Size

Which Filters Can Result in a Renamed Event Being Triggered?

  • DirectoryName
  • FileName

Which Filters Can Result in a Created Event Being Triggered?

  • DirectoryName
  • FileName

And Which Filters Can Result in a Deleted Event Being Triggered?

  • DirectoryName
  • FileName

In case you missed it in the MSDN documentation, you can combine more than one NotifyFilters member by using the bitwise OR operator like so:

...NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite

           | NotifyFilters.FileName | NotifyFilters.DirectoryName;

I Get Two Events When I Create a New File… What’s With That?

Most guides to FileWatcher tend to lead you towards the Changed event. However, using this often leads to multiple events, which is not desirable. Try out the code in this gist to see the two-event behaviour (just copy a file into c:\temp when it’s running). Then try out the code in this other gist, demonstrating how you can use Created with NotifyFilters.FileName to get a single event from a new file in a folder.

A Bit More….Where are the Events for Copying and Moving?

Copied files will trigger Created events in the destination folder, so use NotifyFilters.FileName.

The same applies for moved files but you can also watch the source folder for Deleted events (still using the same NotifyFilter).

The above works for copied and moved folders (using instead, NotifyFilters.DirectoryName), although more code is required to trigger events for any files inside the folder. Seehttps://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx.

Appendix A – Table Detailing NotifyFilters Enumeration from MSDN

Attributes The attributes of the file or folder
CreationTime The time the file or folder was created
DirectoryName The name of the directory
FileName The name of the file
LastAccess The date the file or folder was last opened
LastWrite The date the file or folder last had anything written to it
Security The security settings of the file or folder
Size The size of the file or folder

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

Appendix B – Table of Events You’ll Be Interested In If You’ve Landed Here

Changed Occurs when a file or directory in the specified Path is changed.
Created Occurs when a file or directory in the specified Path is created.
Deleted Occurs when a file or directory in the specified Path is deleted.
Renamed Occurs when a file or directory in the specified Path is renamed.

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

BenHall_io
Software Developer (Senior)
United Kingdom United Kingdom
Left an enjoyable career teaching Computer Science + programming in 2016 and started out in software development.

Currently a Senior Software Engineer, based in Somerset, UK, working with:

C# / ASP.NET
NServiceBus
SQL Server
JavaScript, jQuery etc…

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 8 Dec 2017
Article Copyright 2017 by BenHall_io
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid