Using .NET to Monitor a Directory for Changes

by Robert Greiner on February 8, 2010

.NET’s FileSystemWatcher class can soothe the paranoid control freak in all of us by monitoring a specified folder for different types of file system changes.

The Code

using System;
using System.Text;
using System.IO;

namespace FolderWatcher {
  class Watcher {
    static void Main(string[] args) {
      FileSystemWatcher watcher = new FileSystemWatcher(@"D:\test");
      watcher.IncludeSubdirectories = true;
      watcher.Filter = "";
      watcher.Renamed += new RenamedEventHandler(renamed);
      watcher.Deleted += new FileSystemEventHandler(changed);
      watcher.Changed += new FileSystemEventHandler(changed);
      watcher.Created += new FileSystemEventHandler(changed);
      watcher.EnableRaisingEvents = true;

      Console.ReadKey();
   }

    private static void renamed(object sender, RenamedEventArgs e) {
      Console.WriteLine(DateTime.Now + ": " +
          e.ChangeType + " " + e.FullPath);
    }

    private static void changed(object sender, FileSystemEventArgs e) {
      Console.WriteLine(DateTime.Now + ": " +
          e.ChangeType + " " + e.FullPath);
    }
  }
}

Explanation

FileSystemWatcher watcher = new FileSystemWatcher(@"D:\test");

The core of is application is .NET’s FileSystemWatcher class which monitors directories for file system changes.

watcher.IncludeSubdirectories = true;
watcher.Filter = "";

Tells the FileSystemWatcher object to include changes in subdirectories and raise events on every type of file.

An empty string for the filter will match every file and folder in the watched directory. File types and names can also be added to the filter to create a more fine grained watch.

watcher.Renamed += new RenamedEventHandler(renamed);
watcher.Deleted += new FileSystemEventHandler(changed);
watcher.Changed += new FileSystemEventHandler(changed);
watcher.Created += new FileSystemEventHandler(changed);

Sets the types of file system events the FileSystemWatcher object will respond to. The four possible (and self-explanatory) events are: Renamed, Deleted, Changed, and Created. Each EventHandler takes the method to be executed on that particular event as a paramater in its constructor.

watcher.EnableRaisingEvents = true;

The final piece of the puzzle is to finally tell the FileSystemWatcher to start raising events on the specified directory. Once this statement is executed, we will have a live directory watcher.

Conclusion

I wrote this code as a proof of concept for a new project I am working on. My goal is to create a simple/straightforward directory monitor that anyone can get and start using with very minimal effort.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

{ 1 trackback }

Vote on this article at blogengage.com
February 10, 2010 at 9:45 am

{ 2 comments… read them below or add one }

Chad Stewart February 8, 2010 at 8:37 pm

I like it! I thought wrote a similar article before. It should be worth noting that you can use the Filter property to define a single file to watch.

As an aside, one important thing I learned is that Excel totally hides from the Changed event and instead makes use of Renamed when you save a file. (Plus opens the file without sharing, but that’s a whole other story.)

Robert Greiner February 8, 2010 at 8:38 pm

Thanks. I added some more about the filtering.

I didn’t know that about Excel, thanks for pointing it out.

Leave a Comment

Previous post:

Next post: