How to: Create FileSystemWatcher Component Instances

You create an instance of the FileSystemWatcher component when you want to build file system monitoring into your application. FileSystemWatcher components allow you to connect to directories and watch for specific changes within them, such as the creation of new files, addition of subdirectories, and renaming of files or subdirectories.

You can add an instance of the FileSystemWatcher component to Windows Forms, Web Forms, and component classes. FileSystemWatcher components have no visual user interface. If you add an instance of the FileSystemWatcher component to a visual designer (such as the Windows Forms Designer), the component appears in a small area below the bottom border of the form. This area is known as the component tray and acts as a place to display all of the nonvisual items associated with the form.

Note

Nonvisual classes that inherit from Component support a visual design surface similar to the component tray, on which your FileSystemWatcher component and other components are arranged. The arrangement of items on this designer is not important, as the interface you see will never be displayed to the application's end users.

There are several ways you can create an instance of the FileSystemWatcher component:

  • You can drag an instance of the FileSystemWatcher component from the Components tab of the Toolbox to a form or other designer.

  • You can create an instance of the FileSystemWatcher component in code.

    Note

    Unlike many of the other components on the Components tab of the Toolbox, FileSystemWatcher components are not represented in Server Explorer, so you cannot create instances of the component from that window.

To create an instance of the FileSystemWatcher component from the Toolbox

  1. Access the Components tab of the Toolbox.

  2. Select the FileSystemWatcher icon and drag it to the designer surface for your form or component.

  3. Configure your component. For more information, see How to: Configure FileSystemWatcher Component Instances.

To create an instance of the FileSystemWatcher component programmatically

  1. Create an instance of the FileSystemWatcher base class in code. Your code might look like this:

    Dim myWatcher As New System.IO.FileSystemWatcher()
    
            System.IO.FileSystemWatcher myWatcher =
               new System.IO.FileSystemWatcher();
    
  2. Configure any other necessary properties for your component. For more information, see How to: Configure FileSystemWatcher Component Instances.

See Also

Tasks

How to: Configure FileSystemWatcher Component Instances

File Notification Sample

Concepts

Introduction to Monitoring File System Events