NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Creating 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 include:

You can define handlers for these events 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.

For more information on event handlers, see Event Handling.

To create a handler for a FileSystemWatcher event

  1. Create an instance of the FileSystemWatcher component. For details, see Creating FileSystemWatcher Components.
  2. Set the necessary properties for the component. For details, see Configuring FileSystemWatcher Component Instances.
  3. Use the appropriate AddOn method to create an event handler for your component that will call the appropriate procedure when an entry is written to the log.
    To handle this event Use this method
    Changed AddOnChanged
    Created AddOnCreated
    Deleted AddOnDeleted
    Renamed AddOnRenamed
    Note   For more information on this syntax, see Event Handling.
  4. Create the appropriate procedure that the handler will call and define the code you want to process the entries.
    To handle this event Create this procedure
    Changed FileSystemWatcher1_Changed
    Created FileSystemWatcher1_Created
    Deleted FileSystemWatcher1_Deleted
    Renamed FileSystemWatcher1_Renamed
    
    
    

    The following example shows how to create the handler and create a procedure that interacts with the handler’s parameters.

    [Visual Basic]
    MyWatcher1.AddOnChanged New System.IO.FileSystemEventHandler _
       (AddressOf Me.MyWatcher1_Created)
    Private Sub MyWatcher1_Created(ByVal sender as System.Object, _
       ByVal e as FileSystemEvent)
         Dim watcher As FileSystemWatcher = sender
         Console.WriteLine "File was created in directory: " & watcher.Path
    End Sub
    [C#]
    

See Also

Creating FileSystemWatcher Components | Configuring FileSystemWatcher Component Instances | Introduction to File System Components