Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
0 out of 1 rated this helpful - Rate this topic

Walkthrough: Reacting to File System Events 

The procedures on this page walk you through the process of creating a FileSystemWatcher component, pointing it to a directory on your 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.

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To create the form and components you need for the application

  1. From the New Project dialog box, create a Visual Basic, Visual C#, or Visual J# 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 your local computer and to watch for the creation of files ending 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 your local computer. For example, on a computer 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\
    
    NoteTip

    For the purposes of this example, you can use any directory you want on your 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.

    NoteNote

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

    private void myWatcher_Changed (Object sender, System.IO.FileSystemEventArgs e)
    {
       label1.set_Text("Changes made to: " + e.get_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 on 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 look like 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.";
    }
    
    

    private void myWatcher_Created(Object sender, System.IO.FileSystemEventArgs e)
    {
       label2.set_Text("The file: " + e.get_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 you created in the previous procedure.

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

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

    NoteNote

    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 you created, type a few lines of text, and save it again.

    NoteNote

    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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.