File Notification Sample
This sample uses theclass to respond when files are created, deleted, modified or renamed.
The sample form allows you to set properties of the FileSystemWatcher class, including:
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
Click Download Sample.
The File Download message box appears.
Click Open, and on the left column of the zip folder window, click Extract all files.
The Extraction Wizard opens.
Click Next. You can change the directory that the files will be extracted to, and then click Next again.
Make sure that the Show extracted files check box is selected, and click Finish.
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
Use the controls on the right to change files that are being watched.
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: