This documentation is archived and is not being maintained.

File Notification Sample 

Download sample

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

The sample form allows you to set properties of the FileSystemWatcher class, including:

  • 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 beneath 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 using a CheckBox control displayed as a toggle button.

Security Note   This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To open the sample file in Solution Explorer

  1. Click Download Sample.

    The File Download message box appears.

  2. Click Open, and on the left column of the zip folder window, click Extract all files.

    The Extraction Wizard opens.

  3. Click Next. You can change the directory that the files will be extracted to, and then click Next again.

  4. Make sure that the Show extracted files check box is selected, and click Finish.

  5. Double-click the sample's .sln file.

    The sample solution is displayed in Solution Explorer. You might get a security warning that says the solution location is not trusted. Click OK to continue.

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.


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 are done. 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