Monitoring changes to files
March 10, 2008 | In Development | No CommentsA couple of my recent projects have been evolving at a reasonably fast rate. This has meant I have needed to resource more internal development time at short notice. Currently we use a simple Excel spreadsheet to track individual time allocation, essentially while we wait for a full (third party) time utilisation, tracking and reporting system to come on-line.
It occurred to me that it would be useful to be able to know when the time allocated to my team was changed by someone other than me. It also occurred to me that this ought to be a simple programming problem.
I turned to the .NET FileSystemWatcher, located in the System.IO namespace.
The FileSystemWatcher allows you to monitor the directory structure for changes to files, based on a filtered file list and a set of properties to monitor for changes. In my case I wanted to know when the last write time changed. I set up a simple windows forms application that used a popup to let me know when the file changes. There are a number of options available with the FileSystemWatcher, my contructor code for my watcher object is below.
public FileWatcher(String filePath, String fileName)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = fileName;
watcher.Path = filePath;
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.EnableRaisingEvents = true;
}