The FileSystemWatcher component raises four events depending on the types of changes that occur in the directory it is watching. These events include:
Note You can use the ChangedFilter property to limit the amount of events the Changed event raises. For more information, see Configuring FileSystemWatcher Component Instances.
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
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.
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#]
Creating FileSystemWatcher Components | Configuring FileSystemWatcher Component Instances | Introduction to File System Components