The FileSystemWatcher class is phenomenally powerful—and staggeringly simple.
Start up a new Windows Forms application. Then drop FolderBrowserDialog, FileSystemWatcher, label, listbox, and button controls onto the form.
Setting up Properties:
Set up the name properties of the controls; the button is called chooseButton, the label is folderLabel, the list
box is changesList, the folder browser is folderDialog, and the FileSystemWatcher is simply watcher.
How It Works:
The way this form is going to work is quite simple. The user will click the Choose button and then choose a folder to watch from the dialog that appears. From that point on, we’ll catch events from the watcher and display information on them in the list box. It may sound like a lot of work, but it really isn’t.
First, let’s go ahead and code up the folder-chooser button. Double-click it to open the code editor at the button’s Click event handler, and then key in the following highlighted code:
private void chooseButton_Click(object sender, EventArgs e)
{
if (folderDialog.ShowDialog() == DialogResult.OK)
{
folderLabel.Text = folderDialog.SelectedPath;
watcher.Path = folderDialog.SelectedPath;
watcher.IncludeSubdirectories = true;
watcher.Filter = "*.*";
watcher.EnableRaisingEvents = true;
}
}
What is the meaning of this line:
watcher.Filter = "*.*";
It simply means watch all files with the wildcard of *.*
The next step is to code up the event handlers on the watcher:
All that remains now is to add some code to these handlers. The second parameter to each of these handlers lets you find out information about just why the event fired. In particular, they all have a Name property that refers to the name of the file that triggered the event. You can use this to feed data into the list box about the file that triggered the event. Go ahead and add code to the event handlers to do just that:
private void watcher_Changed(object sender, FileSystemEventArgs e)
{
changesList.Items.Add(e.Name + " changed.");
}
private void watcher_Created(object sender, FileSystemEventArgs e)
{
changesList.Items.Add(e.Name + " was created.");
}
private void watcher_Deleted(object sender, FileSystemEventArgs e)
{
changesList.Items.Add(e.Name + " was deleted.");
}
private void watcher_Renamed(object sender, RenamedEventArgs e)
{
changesList.Items.Add(e.Name + " was renamed.");
}
And that’s all there is to it. Each event just dumps out the name of the file that triggered the event along with some text to relay which event fired, and all into the list box. Run the application now. To make sure you see something, point the app at your C:\ folder and then start an application such as Notepad in Windows; you’ll notice that Windows generates a lot of changes to files when you do anything, especially when you run programs.
Important Note: Before running the program set EnableRaisingEvents property of the watcher to false.
So, for a surprisingly small amount of work you now have a great tool for exploring a myriad of things Windows does behind the scenes.
By: Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]