65.9K
CodeProject is changing. Read more.
Home

A .NET File System Watcher

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.74/5 (10 votes)

Apr 21, 2002

CPOL
viewsIcon

135843

downloadIcon

4404

A utility that monitors a selected directory for changes

Sample Image - FSW1.jpg

Introduction

FSWatcher is a small utility that I wrote for a Distributed Objects class. No, FSWatcher doesn't know how to remote or anything like that! Instead, it just looks on the local drive space for the selected file and waits. Anytime anything changes, FSWatcher notifies the user by checking the box next to the item (if it exists!) and updating the label at the bottom of the dialog box.

This was a pretty simple project, once I knew what to look for. I found everything in the Visual Studio .NET help files. I don't know about you, but that's a first for me!

Sample Image

The meat of the code is here, where the filters and delegates are set up. The code is actually so easy that I won't insult your intelligence by stepping you through it. If you don't understand it, then use it as a guide and try to figure it out.
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = textBox1.Text;
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | 
                   NotifyFilters.DirectoryName | NotifyFilters.FileName;
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnCreated);
fsw.Deleted += new FileSystemEventHandler(OnChanged);
fsw.Renamed += new RenamedEventHandler(OnRenamed);
fsw.EnableRaisingEvents = true;
I had a lot of fun with this... I hope you do, too.