Walkthrough: Reacting to File System Events
The procedures on this page demonstrate creating a FileSystemWatcher component, pointing it to a directory on a local computer, and then using the Filter property to watch only for changes to text files. You will create an event handler that responds when the Created event and Changed event are raised, and you will use a form to display the notifications that result from these events.
In the following procedure, you create a Windows Form and a FileSystemWatcher component instance that will work together to react to directory-level events.
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.
To create the form and components you need for the application
From the New Project dialog box, create a Visual Basic or Visual C# Windows Application, and name it MyWatcher.
From the Windows Forms tab in the Toolbox, drag two Label controls to the designer surface.
Click the Components tab in the Toolbox, and then drag the FileSystemWatcher to the designer surface for your component. By default, this component is named FileSystemWatcher1.
In the following procedure, you set several properties for your component to determine what it watches. These settings cause the component to watch the specified directory on the local computer and to watch for the creation of files that end in the .txt file name extension.
To set properties for your FileSystemWatcher component
Click the FileSystemWatcher component you created in the previous procedure, and view its properties in the Properties window.
Set the component's name to myWatcher.
Use the Path property to set the FileSystemWatcher component to watch a directory on the local computer. For example, on a computer that is running Microsoft Windows 2000, you might enter the following into the Path property to set the component to watch your My Documents directory:
C:\Documents and Settings\yourusername\My Documents\
For the purposes of this example, you can use any directory you want on the local computer.
Set the Filter property to *.txt to have the component watch only those files ending in the .txt file-name extension.
To configure your component
For more information, see Creating Event Handlers in Windows Forms.
Use the following code to display a simple text string that will verify that your event was raised:
Create the event handler for the Created event that specifies what your application should do whenever the component raises this event. For information about creating event handlers, see How to: Create Event Handlers Using the Designer. Add code as shown to display the full path of the newly created file. When finished, you code should resemble the following example.
Save all files, and then build and run your application.
To test your FileSystemWatcher component
Run the application that you created in the previous procedure.
Use Windows Explorer to find the directory that you set your FileSystemWatcher component to watch.
Start Notepad, and then create a new text file. Save this file to the directory you located in step 2 and close the file.
Return to the form. You should see the created message in the label.
Open the text file that you created, type some lines of text, and save it again.
This should raise the Changed event and run the handler you defined.
Return to the form. You should see the change message in the label.