FileSystemWatcher is a special component that has no visible interface and allows your application to monitor changes in the file system. You can use the FileSystemWatcher component to monitor changes in the local computer’s file system, a network drive, and even a remote computer’s file system (as long as the remote machine is running Windows 2000 or a later version). The component exposes a few properties that let you specify what types of changes you want to monitor and the folders/files that will be monitored. Once activated, the FileSystemWatcher component fires an event every time one of the specified items changes.
The items you can monitor are folders and files. You can specify the folders you want to monitor as well as the file types to be monitored. You can also specify the types of actions you want to monitor; each action fires its own event. The actions you can monitor are the creation, deletion, and renaming of a file or folder and the modification of a file. The corresponding events are appropriately named Changed, Created, Deleted, and Renamed. There’s also a special event, the Error event, that is fired when too many changes occur and the FileSystemWatcher component can’t keep track of them all. (The internal buffer overflows, and this condition is signaled with the Error event.) It’s not recommended that you monitor very large folders, or folders with lots of activity.
To use a FileSystemWatcher component in your project, open the Components tab of the Toolbox and double-click the FileSystemWatcher component’s icon. An instance of the component will be placed in your project, and you can set the following properties in the Properties window.
This property determines the types of changes you want to monitor. It can have one of the values shown in Table 11.8, which are the members of the IO.NotifyFilters enumeration.
You can combine multiple types of changes by using the Or operator. The following statement prepares the FileSystemWatcher1 component to monitor for changes in the date and time of a file’s last write and last access:
FileSystemWatcher1.NotifyFilter = _
The NotifyFilter property can also be set in the Properties window, but you can’t combine multiple notification types there.
Table 11.8: The NotifyFilters Enumeration
|Attributes||The attributes of the file or folder|
|CreationTime||The date of the file’s or folder’s creation|
|DirectoryName||The directory name|
|LastAccess||The date of the file’s or folder’s last access|
|LastWrite||The date of the file’s or folder’s last edit|
|Security||The security settings of the file or folder|
|Size||The size of the file or folder|
Set the Path property to the path you want to monitor. The component will watch for changes in files in the specified path. If you want to include the path’s subfolders, set the Include Subdirectories property to True. The default value of this property is False.
This property filters the files you want to monitor through a string with wildcards. A Filter value of *.txt tells the component to monitor for changes in text files only. The default value of the Filter property is *.*, which includes all the files. An empty string will also have the same effect. Notice that you can’t specify multiple extensions with the Filter property.
To start monitoring for changes in the file system, set the EnableRaisingEvents property to True. While the EnableRaisingEvents property is True, the FileSystemWatcher component fires an event for the changes you have specified through its properties. To stop monitoring the changes in the file system, set this property to False.
To notify your application about the changes, the FileSystemWatcher component raises the following events, which you can handle from within your code: Changed, Created, Deleted, and Renamed. Like all events, they include two arguments: the sender and the e argument. The second argument of these events carries information about the type of the change through the ChangeType property. The e.ChangeType property’s value is a member of the IO.WatcherChangeTypes enumeration: All, Changed, Created, Deleted, and Renamed. The e.FullPath and e.Name properties are the path and filename of the file that was changed, created, or deleted. In the case of a folder, use the FullPath property to retrieve the name of the changed folder. Finally, the Renamed event’s argument exposes the OldFullPath and OldName members, which let you retrieve the old path and name of the renamed file.
You can write a common event handler for the Changed, Created, and Deleted events because they share the same arguments. The Rename event must have its own handler, because the e argument is of a different type.
All the changes detected by the FileSystemWatcher component are stored in an internal buffer, which can overflow if too many changes take place in a short period of time. To avoid overflowing the buffer, you should limit the number of files you monitor by setting the Filter and Path properties appropriately. You should always limit the type of changes. (You’ll rarely have to monitor for all types of changes in a folder.) If the buffer overflows, the Error event will be raised.
In this event’s handler, you can increase the size of the buffer by setting the InternalBufferSize property. You can double the buffer’s size from within the Error event handler to prevent the loss of additional events by using the following statement:
FileSystemWatcher1.InternalBufferSize = _
2 * FileSystemWatcher1.InternalBufferSize