FileSystemWatcher

Framework: NET

FileSystemWatcher monitors a directory for changes. A program needs process new files written to the directory by other programs. This class provides a way to monitor a directory for file changes, creations, deletions and renames.

Tutorial

Steps

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.

FileSystemWatcher properties
Program that uses FileSystemWatcher instance: C#

using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    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));
	}
    }
}
Program icon

The four MessageBox.Show calls display the events being triggered in the program. Once the program is run, you can change files in the C:\watch directory specified above and dialog boxes will display what events are being triggered.

MessageBox.Show Examples

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.

Note:You may need special code to filter Changed events. This depends on your application.

Timer

Programming tip

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:You can then call methods based on the changed detected. Also, you could use the FileInfo type as well as the Timer type for this.

Timer TutorialFileInfo ExamplesFileInfo Length, Get File Size

Summary

C# language

We examined the FileSystemWatcher class in the Windows Forms platform and C# 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.


C#: Windows