Iím going to demonstrate the use of the FileSystemWatcher class by creating a Windows Forms application
that displays new pictures as theyíre added to the userís Pictures folder. To do that,
I have a blank Windows Forms application here and Iím going to add a PictureBox to it.
Letís make this shaped more like a traditional picture, and then Iíll resize the PictureBox
to take up the entire form.
Iím going to generate the standard Load handler here. First Iím going to add the System.IO
namespace which contains our FileSystemWatcher class.
To identify the folder that we need to monitor, Iím going to create a string that is based
on two separate folders. The first is the userís profile folder, which contains the
Documents and Pictures folder. I can get that by calling the Environment.GetEnvironmentVariable
method, and specifying the USERPROFILE variable. Iím calling System.IO.Path.CombineóI want
to add on the Pictures sub-folder. The Path.Combine method will take care of combining the base
folder and the sub-folder in the correct way, so I donítí have to worry about backslashes
or the traditional work that would be done if I were manually concatenating the strings.
Now Iím ready to create my FileSystemWatcher instance; Iíll call it fsw. Iíll specify
the watched String that I just created. To allow the user that the correct folder is
being monitored, Iíll set the Form title to the folder.
Now the FileSystemWatcher has several different properties you can define that change the
way it monitors directories. I want to make sure that any subfolder of the Pictures folder
is monitored, so Iíll set FileSystemWatcher.IncludeSubdirectories to True.
You can setup NotifyFilters to control exactly which changes throw events. You can see here
that the NotifyFilters constant provides several different types of attributes that can be
monitored. These are all fairly straightforward. Iím going to monitor two of them to be sure
that I catch any new files. NotifyFilters.FileName and NotifyFilters.LastWrite. Because Iím
putting two different attributes there, this might actually throw two eventsóthatís okay
because of the way the program works. These are actually binary values. To use both of
those at the same time, I need to use the Or Boolean operator.
Now that the FileSystemWatcher is correctly configured, I can add the event handler. Now
that the FileSystemWatcher is correctly configured, I can add the event handler. Note that I haven't
created this event yet, but Iíll do that next. The event that weíre responding to
is FileSystemWatcher.Changed. Iíll specify the address of a method (which doesnít exist
yet) as fsw_Changed.
Now that Iíve created the event handler, thereís one last property of FileSystemWatcher
I need to specify, and thatís EnableRaisingEvents. Iíll set that to True, and that actually
enables events to be responded to. If it were set to False, events could occur but the event
handler would not be called.
Now Iím going to create the event handler. As with all event handlers, it requires two
parameters. The first is always the sender, and thatís an object. You donít often use
that. The second is a derivative of FileSystemEventArgs. By convention, itís always called e.
FileSystemEventArgs contains information about the event that just occurred, and it contains
the path of the file that just changed. I can set PictureBox1.ImageLocation to e.FullPath.
Notice that the instance of FileSystemEventArgs contains a couple of other properties that
might be useful. ChangeType specifies either a file rename or a file change. In this case,
Iím not concerned about itóanything it notifies me about, Iím interested in. FullPath is
the full path of the changed file; thatís the one Iím going to use. FileName is just
the name of the file without the path information. I specify FullPath there, and itís ready
to run.
I have some vacation photos here in my Pictures folder. I will make a copy of one of them,
which generates a new file, triggering the FIleSystemWatcher event handler. You can see
that as I copy each picture, it detects the change and displays the picture correctly.