Export (0) Print
Expand All

Walkthrough: Reacting to File System Events

The procedures in this topic walk you through the process of creating a FileSystemWatcher component, pointing it to a directory on your local computer, and then using the FileSystemWatcher.Filter property to watch only for changes to text files. You will create an event handler that responds when the FileSystemWatcher.Created event and FileSystemWatcher.Changed event are raised, and you will use a form to display the notifications that result from these events.

Create the form and components you need for the application

In this procedure, you create a Windows Form and a FileSystemWatcher component instance that will work together to react to directory-level events.

  1. From the New Project dialog box, create a Visual Basic or 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 icon to the designer surface for your component. By default, this component is named FileSystemWatcher1.

To set properties for your FileSystemWatcher component

In this procedure, you set several properties for your component to determine what it will watch. 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.

  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\
    
    Tip   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.

Write the necessary code for your component

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

  1. Double-click the FileSystemWatcher component. The Code Editor appears and a default event handler for the Changed event appears.
    Note   For more information, see Event Handling in Windows Forms.
  2. Use the following code to display a simple text string that will verify that your event was raised:
    ' Visual Basic
    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
    
    // C#
    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 on creating event handlers, see Creating Event Handlers on the Windows Forms Designer. Add code as shown to display the full path of the newly created file.
    ' Visual Basic 
    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
    
    //C#
    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.

To test your FileSystemWatcher component

In this 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.

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

See Also

Creating FileSystemWatcher Component Instances | Introduction to Monitoring File System Events | Label Control (Windows Forms)

Show:
© 2015 Microsoft