Share via


File Notification Sample

This sample uses the FileSystemWatcher class to respond when files are created, deleted, modified or renamed.

To get samples and instructions for installing them

  • Do one or more of the following:

    • On the Help menu, click Samples.

      The Readme displays information about samples.

    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.

    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.

For more information, see Visual Studio Samples.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To run this sample

  1. Press F5.

  2. Use the controls on the right to change files that are being watched.

  3. Use Windows Explorer to change files that are being watched. It makes no difference how the file changes are accomplished.

Demonstrates

The sample form enables you to set properties of the FileSystemWatcher class. This includes the following:

  • Path   The file system path to be "watched" by the object.

  • Filter   The types of files to be watched. The default is *.*.

  • NotifyFilter   The types of changes to watch for. This property can be zero or more values from the System.IO.NotifyFilters enumeration, combined together. By default, the FileSystemWatcher detects FileName, DirectoryName, and LastWrite changes.

  • IncludeSubdirectories   Indicates whether the monitoring should include directories underneath the selected path.

  • EnableRaisingEvents   If true, the object monitors for file-system changes and raises events as they occur. Basically, this is the "on/off switch". In this demonstration, this property is controlled by using a CheckBox control displayed as a toggle button.

The controls on the form let you generate events by creating, deleting, renaming, and modifying files. This component tray of the form includes a FileSystemWatcher component, found in the Components tab on the Toolbox. Using the component makes it simple to set properties at design time by using the Properties window.

The Create, Rename, Modify, and Delete Sample File buttons on the form are used to generate the FileSystemWatcher events. These buttons create temporary files in the selected folder. Use the Delete All Sample Files button to clean up all remaining sample files when you have finished. Five of the FileSystemWatcher events are handled in this sample:

  • Created, Changed, and Deleted   These three events are all handled by the same event handler, HandleChangedCreatedDeleted. The details of the event are displayed on the form.

  • Renamed   The old and new names are displayed.

  • Error   The exception message is displayed.

See Also

Reference

FileSystemWatcher

NotifyFilters