DynDeveloper.com

DynDeveloper.com

Sign Up Now! Log In
Micah 1/23/2012 9:02:45 AM

Windows Service - FileSystemWatcher example code

* This article, and all our great .NET Development documentation, Is available on the .NET Development menu

I've needed a piece of code like this for some time.

Recently I was asked to write an application that would watch a directory and respond to files that were saved there. We'd need to pick them up, process them, and then move them to a 'done' folder. Standard Dyn Developer stuff.

Normally, I'd write a console application and schedule it with SQL scheduler to sweep the folder every x minutes.

At some point I became aware of the System.IO.FileSystemWatcher class. The FileSystemWatcher class 'Listens to the file system change notifications and raises events when a directory, or file in a directory, changes'. Cool, right? The example below is a complete Windows Service application that uses the FileSystemWatcher class to monitor a directory and to respond to files that are saved in that directory.

The paragraphs below are from the MSDN remarks for the FileSystemWatcher class:

Use FileSystemWatcher to watch for changes in a specified directory. You can watch for changes in files and subdirectories of the specified directory. You can create a component to watch files on a local computer, a network drive, or a remote computer.

To watch for changes in all files, set the Filter property to an empty string ("") or use wildcards ("*.*"). To watch a specific file, set the Filter property to the file name. For example, to watch for changes in the file MyDoc.txt, set the Filter property to "MyDoc.txt". You can also watch for changes in a certain type of file. For example, to watch for changes in text files, set the Filter property to "*.txt".

There are several types of changes you can watch for in a directory or file. For example, you can watch for changes in Attributes, the LastWrite date and time, or the Size of files or directories. This is done by setting the NotifyFilter property to one of the NotifyFilters values. For more information on the type of changes you can watch, see NotifyFilters.

You can watch for renaming, deletion, or creation of files or directories. For example, to watch for renaming of text files, set the Filter property to "*.txt" and call the WaitForChanged method with a Renamed specified for its parameter.

The Windows operating system notifies your component of file changes in a buffer created by the FileSystemWatcher. If there are many changes in a short time, the buffer can overflow. This causes the component to lose track of changes in the directory, and it will only provide blanket notification. Increasing the size of the buffer with the InternalBufferSize property is expensive, as it comes from non-paged memory that cannot be swapped out to disk, so keep the buffer as small yet large enough to not miss any file change events. To avoid a buffer overflow, use the NotifyFilter and IncludeSubdirectories properties so you can filter out unwanted change notifications.

Version: Unknown or N/A
Section: .NET Development