Monitoring the File System – Visual Basic.NET

You can use the FileSystemWatcher class to respond to file system events, such as a user creating, deleting, or removing a file. This might be useful if, for example, you wrote a program that automatically adds metadata to images copied to a particular folder, or automatically process text log files.

How to Monitor the File System

Responding to file system events requires three elements:

  • An instance of FileSystemWatcher configured with the properties to monitor
  • A method that runs when a change occurs
  • An event handler that specifies the method to run when the FileSystemWatcher event occurs

Once you have created these three elements, set FileSystemWatcher.EnableRaisingEvents to true to begin monitoring.

FileSystemMonitor Events

This table lists the three events you can respond to using the FileSystemWatcher class. You can create a single event handler for each of these types of events, and check the FileSystemEventArgs.ChangeType property in the event handler to determine the type of change event.



Changed Called when a new file is created, when a file is updated, or when a file’s attributes (other than its filename) are changed.
Renamed Called when a file is moved or renamed
Deleted Called when a file is removed from the file system
Created Called when a new file is added to the file system

Video Demonstration

In this video, I show you exactly how to use the FileSystemMonitor class using a WPF application. Be sure to watch it in HD, full-screen.

Here’s the transcript to that video:

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.

FileSystemMonitor Sample Project Source Code

Download the WPF FileSystemMonitor sample project: FileSystemMonitor Sample WPF Application.

Return to the .NET Framework Fundamentals Tutorials Table of Contents.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>