Monitor the file system with FileSystemWatcher in C# .NET

In this post we’ll look at how you can use the FileSystemWatcher object to monitor the Windows file system for various changes.

A FileSystemWatcher object enables you to be notified when some change occurs in the selected part of the file system. This can be any directory, such as “c:\” or any subdirectory under the C: drive. So if you’d like to make sure you’re notified if a change occurs on e.g. “c:\myfolder” – especially if it’s editable by your colleagues – then FileSystemWatcher is a good candidate.

Consider the following Console application:

class Program
{
	static void Main(string[] args)
	{
		RunFirstExample();
		Console.ReadKey();
	}

	private static void RunFirstExample()
	{
		FileSystemWatcher watcher = new FileSystemWatcher();
		watcher.Path = @"c:\mydirectory";
		watcher.Created += watcher_Created;
		watcher.Deleted += watcher_Deleted;
		watcher.EnableRaisingEvents = true;			
	}

	static void watcher_Deleted(object sender, FileSystemEventArgs e)
	{
		Console.WriteLine("File deleted. Name: {0}", e.Name);
	}

	static void watcher_Created(object sender, FileSystemEventArgs e)
	{
		Console.WriteLine("File created. Name: {0}", e.Name);
	}
}

In RunFirstExample we specify that we’re interested in monitoring the c:\mydirectory directory. Then we subscribe to the Created and Deleted events which represent the insertion of a new file and the deletion of an existing file in the directory. The FileSystemEventArgs has a couple of properties to show the event type, such as “Created” or “Deleted” in a WatcherChangeTypes enumeration, the file name and the file full path. We then start running the monitoring process by setting EnableRaisingEvents to true.

Note the call to Console.ReadKey(); in Main. The process won’t magically run in the background once Main is done. The FileSystemWatcher must sit within a continuous process, such as a Windows service.

Run the above code and insert a new file into the monitored directory. Then delete the file and watch the console output. Insertion example:

File creation monitored by filesystemwatcher

File deleted:

File deletion monitored by filesystemwatcher

The FileSystemWatcher object also lets you monitor a given directory for file updates. The following code is very similar to the one referred to in the above link. The difference is that we subscribe to the Renamed event:

static void Main(string[] args)
{
	RunUpdateExample();
	Console.ReadKey();
}

private static void RunUpdateExample()
{
	FileSystemWatcher watcher = new FileSystemWatcher();
	watcher.Path = @"c:\mydirectory";
	watcher.Renamed += watcher_Renamed;
	watcher.EnableRaisingEvents = true;
}

static void watcher_Renamed(object sender, RenamedEventArgs e)
{
	Console.WriteLine("File updated. Old name: {0}, new name: {1}", e.OldName, e.Name);
}

Again, we have the Console.ReadKey(); in Main to make sure that the console app doesn’t just quit, otherwise the file system watcher process dies.

If you run this code and rename a file in the monitored directory you may see an output similar to the following:

File name change monitored by filesystemwatcher

It can happen that there are more changes than the FileSystemWatcher object can handle. In that case FileSystemWatcher raises the Error event which you can subscribe to as follows:

static void Main(string[] args)
{
	RunErrorExample();
	Console.ReadKey();
}

private static void RunErrorExample()
{
	FileSystemWatcher watcher = new FileSystemWatcher();
	watcher.Path = @"c:\mydirectory";
	watcher.Error += watcher_Error;
	watcher.EnableRaisingEvents = true;
}

static void watcher_Error(object sender, ErrorEventArgs e)
{
	Exception ex = e.GetException();
	Console.WriteLine(ex.Message);
	if (ex.InnerException != null)
	{
		Console.WriteLine(ex.InnerException);
	}
}

FileSystemWatcher has a couple more properties that can be interesting.

Consider the following code:

static void Main(string[] args)
{
	RunVariousFeaturesExample();
	Console.ReadKey();
}

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.

Advertisement

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:

WordPress.com Logo

You are commenting using your WordPress.com 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

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

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

%d bloggers like this: