Monitor the file system with FileSystemWatcher in C# .NET Part 4: other features

In the first three parts of this mini-series – starting here – we looked at how to subscribe to the events raised by a FileSystemWatcher object. FileSystemWatcher has a couple more properties that can be interesting.

Consider the following code:

static void Main(string[] args)

private static void RunVariousFeaturesExample()
	FileSystemWatcher watcher = new FileSystemWatcher();
	watcher.Path = @"c:\mydirectory";
	watcher.Filter = "*.txt";
	watcher.IncludeSubdirectories = true;
	watcher.NotifyFilter = NotifyFilters.Security | NotifyFilters.Size;
	watcher.Changed += watcher_Changed;
	watcher.EnableRaisingEvents = true;

static void watcher_Changed(object sender, FileSystemEventArgs e)
	Console.WriteLine("A change occurred in the monitored directory. Change type: {0}, file name: {1}", e.ChangeType, e.Name);

We set the path to be monitored as usual. We can filter the file names to be monitored. Here we’re interested in text files only. We also specify that we want to monitor all subfolders of the “Path” directory through the IncludeSubdirectories property. With NotifyFilter we can further refine the cases when we want to be notified of a file change. Here we want to be notified if either the file size changes or the security properties have been updated. Then we subscribe to the generic Changed event which is raised in case an existing file changes.

In the below example I’ve updated the security settings of a file and it successfully raised the Changed event:

File change monitored by filesystemwatcher

Read all posts dedicated to file I/O here.


About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: