You use the FileSystemWatcher component to monitor a file system and react when changes to it occur. This makes it possible for you to quickly and easily launch business processes when certain files or directories are created, modified, or deleted. For example, suppose you and a group of coworkers are collaborating on a document that is stored on a shared directory on your server. Using the FileSystemWatcher component, you can easily program your application to watch for changes to the shared directory. When a change is detected, the component can run processing that notifies each of the group members via e-mail.
You can configure the component to watch either an entire directory and its contents or a specific file or set of files within a given directory. The FileSystemWatcher component raises an event whenever a file or directory is created, deleted, renamed, or changed in some other way. The types of changes that the component monitors include changes in the directory’s or file’s properties, size, last write time, last access time, and security settings.
When changes occur in a directory or file your application is monitoring, one or more events are raised, stored in a buffer, and ultimately passed to your component for processing. The FileSystemWatcher component gives you access to the following events:
Note Although some common occurrences, such as copying or moving a file, do not correspond directly to an event, these occurrences do cause events to be raised. When you copy a file, the system raises a Created event in the directory to which the file was copied but does not raise any events in the original directory. When you move a file, the server raises two events: a Deleted event in the source directory, followed by a Created event in the target directory.
Each change to a file in a directory raises a separate event. If a user makes multiple changes to a single file, the application raises a separate event for each change. For example, if you are watching a directory that contains five files and the security permissions for all of the files suddenly change, you will receive five Changed events. If the user renames and assigns new security permissions to each of those files, you will receive 10 event notifications — a Changed event and a Renamed event for each of the five files.
You must meet the following restrictions to use the FileSystemWatcher component:
Creating FileSystemWatcher Component Instances | Configuring FileSystemWatcher Component Instances | Creating Handlers for File System Events | File Changes on High-Volume Systems