FileSystemWatcher (exploring a myriad of things Windows does behind the scenes)

toraj58 1 Tallied Votes 387 Views Share

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]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileSystemWatcher
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        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;
                
            }
        }

        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.");
        }

    }
}
nenadnikolic 0 Newbie Poster

Hi toraj58,

So, it is small amount of work, but somehow in a change List, there is no any event shown.
What is the problem?

I also changed EnableRaisingEvents property of the watcher to false, and nothing again.

This is interesting and useful for me, so please explain me what is the problem.

Thank's, by!

DdoubleD 315 Posting Shark

Cool... I hadn't seen this class used before! I noticed when I tried to delete a subfolder I got a system error... I have not checked the MSDN doc on this yet so maybe that is a given...

I would like to suggest that unless you are going to use default control names (eg. changeList instead of listBox1) that you go ahead and post the designer portion of the code too.

Good information though! Cheers!

nenadnikolic 0 Newbie Poster

Hi again,

I am using control names just like you sugested in your code, that's not the problem.

Olso, in all watcher events I put simple code to add "Hello" in changeList, instead event name "e.Name + ... ", and nothing again.

I think that somehow filesSystemWatcher cannot see eny event of pointed file.

I'l see in MSDN about FileSystemWatcher...

Thank's anyway!

If I found something tou will know...

By

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.