Compartilhar via


How to: Create Handlers for File System Events 

The FileSystemWatcher component raises four events depending on the types of changes that occur in the directory it is watching. These events are:

  • Created — raised whenever a directory or file is created.

  • Deleted — raised whenever a directory or file is deleted.

  • Renamed — raised whenever the name of a directory or file is changed.

  • Changed — raised whenever changes are made to the size, system attributes, last write time, last access time, or NTFS security permissions of a directory or file.

    NoteNote

    You can use the NotifyFilter property to limit the amount of events the Changed event raises. For more information, see How to: Configure FileSystemWatcher Component Instances.

For each of these events, you can define handlers that automatically call methods in your code when a change occurs. An event handler is a method that is bound to an event for a component, form, or control. Each event handler provides two parameters that allow you to handle the event properly — the sender, which provides an object reference to the object responsible for the event, and the e parameter, which provides an object for representing the event and its information.

There are several ways you can create an event handler for an instance of the FileSystemWatcher component, but the simplest way is to let Visual Studio create most of the code for you automatically. When you double-click your FileSystemWatcher component instance in the designer, Visual Studio opens the Code Editor and creates an event handler for both the Changed event and the blank procedure it calls. You can then fill in the processing that you want to occur in the procedure. You can also create your own handlers to handle the other events.

For more information on event handlers, see Handling and Raising Events.

To create a default handler for the Changed event

  1. Create an instance of the FileSystemWatcher component. For more information, see How to: Create FileSystemWatcher Component Instances.

  2. Set the necessary properties for the component. For more information, see How to: Configure FileSystemWatcher Component Instances.

  3. From the designer, double-click the FileSystemWatcher component for which you want to create a handler.

    NoteNote

    The Code Editor appears and two items are added to your code: the handler that creates and registers a delegate and calls the procedure, and a blank procedure for the Changed event.

  4. In the blank procedure for the Changed event, define code to take action when this event is called.

To create a handler for a FileSystemWatcher event

  1. Create an instance of the FileSystemWatcher component. For more information, see How to: Create FileSystemWatcher Component Instances.

  2. Set the necessary properties for the component. For more information, see How to: Configure FileSystemWatcher Component Instances.

  3. Create the appropriate procedure that will be called and define the code you want to process the entries. The following example shows an event handler for the Changed event.

    Private Sub myWatcher_Changed(ByVal sender As System.Object, _
       ByVal e As System.IO.FileSystemEventArgs)
        Dim pathChanged As String
        pathChanged = e.FullPath
    End Sub
    
    private void myWatcher_Changed(object sender,
    System.IO.FileSystemEventArgs e)
    {
        string pathChanged = e.FullPath;
    }
    
    private void myWatcher_Changed(SObject sender, 
    System.IO.FileSystemEventArgs e)
    {
       String pathChanged = e.get_FullPath();
       MessageBox.Show(pathChanged);
    }
    
  4. Assign an instance of FileSystemEventHandler to the event for your component. This example adds the Changed event handler defined in step 3.

    AddHandler myWatcher.Changed, _
       New System.IO.FileSystemEventHandler(AddressOf Me.myWatcher_Changed)
    
    myWatcher.Changed +=
       new System.IO.FileSystemEventHandler(this.myWatcher_Changed);
    
    myWatcher.add_Changed(
       new System.IO.FileSystemEventHandler(this.myWatcher_Changed));
    

See Also

Tasks

How to: Create FileSystemWatcher Component Instances
How to: Configure FileSystemWatcher Component Instances

Concepts

Introduction to Monitoring File System Events