Walkthrough: Reacting to File System Events

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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.

Note Note

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 Working with Settings.

To create the form and components you need for the application

  1. From the New Project dialog box, create a Visual Basic or Visual C# Windows Application, and name it MyWatcher.

  2. From the Windows Forms tab in the Toolbox, drag two Label controls to the designer surface.

  3. 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

  1. Click the FileSystemWatcher component you created in the previous procedure, and view its properties in the Properties window.

  2. Set the component's name to myWatcher.

  3. 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.

  4. Set the Filter() property to *.txt to have the component watch only those files ending in the .txt file-name extension.

In the following procedure, you define two event handlers for your component that define the processing that should occur whenever the Changed() and Created() events are raised.

To configure your component

  1. Double-click the FileSystemWatcher component. The Code Editor appears and a default event handler for the Changed() event appears.

    Note Note

    For more information, see Creating Event Handlers in Windows Forms.

  2. Use the following code to display a simple text string that will verify that your event was raised:

    Private Sub myWatcher_Changed(
        ByVal sender As System.Object, 
        ByVal e As System.IO.FileSystemEventArgs
      ) Handles myWatcher.Changed
       Me.Label1.Text = "Changes made to: " & e.FullPath
    End Sub

    private void myWatcher_Changed(object sender, 
       System.IO.FileSystemEventArgs e)
       label1.Text = "Changes made to: " + e.FullPath;

  3. 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.

    Private Sub myWatcher_Created(
        ByVal sender As System.Object, 
        ByVal e As System.IO.FileSystemEventArgs
      ) Handles myWatcher.Created
       Me.Label2.Text = "The file: " & e.FullPath & 
          " has been added to your directory"
    End Sub

    private void myWatcher_Created(object sender,
       System.IO.FileSystemEventArgs e)
       label2.Text = "The file: " + e.FullPath + 
          " has been added to your directory.";

  4. Save all files, and then build and run your application.

In the following procedure, you will manually make changes in the directory your component is watching so that you can force the event handler to raise the Changed() and Created() events.

To test your FileSystemWatcher component

  1. Run the application that you created in the previous procedure.

  2. Use Windows Explorer to find the directory that you set your FileSystemWatcher component to watch.

  3. Start Notepad, and then create a new text file. Save this file to the directory you located in step 2 and close the file.

    Note Note

    This should raise the Created() and Changed() events and run the handler you defined.

  4. Return to the form. You should see the created message in the label.

  5. Open the text file that you created, type some lines of text, and save it again.

    Note Note

    This should raise the Changed() event and run the handler you defined.

  6. Return to the form. You should see the change message in the label.