How can I monitor a Windows directory for changes? How can I monitor a Windows directory for changes? windows windows

How can I monitor a Windows directory for changes?


Use a FileSystemWatcher like below to create a WatcherCreated Event().

I used this to create a Windows Service that watches a Network folder and then emails a specified group on arrival of new files.

    // Declare a new FILESYSTEMWATCHER    protected FileSystemWatcher watcher;    string pathToFolder = @"YourDesired Path Here";    // Initialize the New FILESYSTEMWATCHER    watcher = new FileSystemWatcher {Path = pathToFolder, IncludeSubdirectories = true, Filter = "*.*"};    watcher.EnableRaisingEvents = true;    watcher.Created += new FileSystemEventHandler(WatcherCreated);    void WatcherCreated(object source , FileSystemEventArgs e)    {      //Code goes here for when a new file is detected    }


FileSystemWatcher is the right answer except that it used to be that FileSystemWatcher only worked for a 'few' changes at a time. That was because of an operating system buffer. In practice whenever many small files are copied, the buffer that holds the filenames of the files changed is overrun. This buffer is not really the right way to keep track of recent changes, since the OS would have to stop writing when the buffer is full to prevent overruns.

Microsoft instead provides other facilities (EDIT: like change journals) to truly capture all changes. Which essentially are the facilities that backup systems use, and are complex on the events that are recorded. And are also poorly documented.

A simple test is to generate a big number of small files and see if they are all reported on by the FileSystemWatcher. If you have a problem, I suggest to sidestep the whole issue and scan the file system for changes at a timed interval.


If you want something non-programmatic try GiPo@FileUtilities ... but in that case the question wouldn't belong here!