
Your Windows Forms program written in the C# language has a special directory that it must monitor for changes. For example, your program must process new files written to the directory by other programs. The FileSystemWatcher class provides an excellent way to monitor a directory for file changes, file creations, file deletions, and file renames.
This C# tutorial demonstrates the FileSystemWatcher class. It uses Windows Forms.
Before you can write any code, you must set up your Windows Forms program properly. Open the Toolbox and double-click on the FileSystemWatcher icon; then, in the bottom tray, select Properties on the fileSystemWatcher1 label. You can specify the directory you want to monitor for changes; in this example, we use a directory named C:\watch. Next, in the Properties panel, click on the lightning bolt and add four event handlers.

Program that uses FileSystemWatcher instance [C#]
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication23
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
// Occurs when the contents of the file change.
MessageBox.Show(string.Format("Changed: {0} {1}", e.FullPath, e.ChangeType));
}
void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
// FullPath is the new file's path.
MessageBox.Show(string.Format("Created: {0} {1}", e.FullPath, e.ChangeType));
}
void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e)
{
// FullPath is the location of where the file used to be.
MessageBox.Show(string.Format("Deleted: {0} {1}", e.FullPath, e.ChangeType));
}
void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e)
{
// FullPath is the new file name.
MessageBox.Show(string.Format("Renamed: {0} {1}", e.FullPath, e.ChangeType));
}
}
}What the MessageBox.Show invocations do. The four MessageBox.Show calls in this program display the events being triggered in the program. Once the program is compiled and is executed, you can change files in the C:\watch directory specified above and dialog boxes will display what events are being triggered.
MessageBox.Show ExamplesMultiple Changed events. With the FileSystemWatcher, you may have a problem with receiving multiple Changed events for a single file change. This must be due to a technical detail about how some programs write files; in any case, you may need special code to filter Changed events.
One of my favorite techniques for monitoring a program for changes is using a Timer instance and simply doing a scan on a regular interval. In this way, you could simply scan a directory every five seconds for changes and then call methods based on the changed detected; you could use the FileInfo type as well as the Timer type for this.
Timer Tutorial FileInfo Examples FileInfo Length, Get File Size
We examined the FileSystemWatcher class in the Windows Forms platform and C# programming language. This type presents an excellent way to persistently monitor a directory for changes, without lots of custom code; further, its clear event-driven model can help streamline your program's operations.
Windows Forms