This article talks about how to set a watch on a file or directory, and take custom actions when that file is modified.
These tasks can be accomplished by using the .NET class FileSystemWatcher. You can watch for changes in files and subdirectories of the specified directory. The component can watch files on a local computer, a network drive, or a remote computer.
Let’s go a little deeper into the class and find out more about it, by taking a look at its methods and properties. The first thing we need to do after instantiating the class is set its Path property to the path of the directory that we’d like to watch. If we’d like to watch a file and not a directory, we should set the Path property to the file’s parent directory and the Filter property to the name of the file that we would like to watch.
But what happens when we rename the directory? Would the FilsSystemWatcher still watch the old path? The answer is no. It will automatically watch the new directory’s name. Still, if you look at the Path property, it would still hold the old name of the folder. The next important property is the Filter property that I’ve mentioned above. This property sets the file or type of file contained in the directory specified by Path to watch. The default value of the Filter property is “*.*”. This means that the FileSystemWatcher object will watch all files that have an extension, whatever the extension or the file name is. Consider that if you have a file called TEST, it would not be watched, since it doesn’t respect the pattern “*.*”(it has no extension). To watch for a specific file, set the Filter property to that file’s name(Ex.: “Test.txt”); if you would like to watch all files in that directory, set the Filter to “*”. Finally, if you would like to watch for files that have a certain extension, set the Filter to “*.[extension]”(Ex.: watch for text files – ”*.txt”).
If you would like to watch also the subdirectories of the directory entered in Path, set the IncludeSubdirectories property to true.
Also, you have an option to specify the type of change actions that will be watched. You can change them though the NotifyFilter property. The default types of changes are FileName (the name of the file), DirectoryName (the name of the directory) and LastWrite (last modified date). You can modify them by creating a combination of the others members of the NotifyFilters enumeration. These members are:
Attributes (raises an event when the folder’s attributes are changed), CreationTime (watch the time the file/folder was created – raises an event when you create/copy a file into the directory), DirectoryName (raises an event when a directory is renamed), FileName (raises an event when a file is renamed), and LastAccess (raises an event when the file / folder is opened), LastWrite (raises an event when the file is modified),Security (raises an event when the file’s or folder’s security changed) and Size(raises an event when the file’s size changes). Let’s suppose that we have a FileSystemWatcher object on a directory and its Filter is the file “test.txt”. If we have all the NotifyFilters watching for changes and we will open the file, type something, save and close it. The LastWrite NotifyFilter will raise an event. But also, the Size filter will also raise an event. The same thing happens if you change the security of a folder. You will have all the files in that folder raising a Security event.
You should be careful when watching a large amount of files, and change a attribute that will affect most of them.
You will risk a buffer overflow and all the events raised will be lost. You can prevent this from happening by increasing the size of the buffer through the InternalBufferSize property,but this can seriously affect your application’s performance.
The FileSystemNotification class also has few events for handling the events raised. These actions are:
- Changed – raised when changes are made to the size, system attributes, last write time, last access time, or security permissions of a file or directory in the directory being monitored
- Created – raised when a new file/directory is created or a file/directory is copied or moved into the watched directory
- Deleted – raised when a file or a directory is deleted
- Renamed – raised when a file/directory is renamed
- Error – raised when a buffer overflow error occurs
Along with this article, you have a simple application that prints the actions you’ve taken to a certain directory.
Below, you have the simple way to set up a FileSystemWatcher. Note that tb_path and tb_type are textboxes, where you can select the path and type of files to watch.
watch.Path = tb_path.Text; watch.Filter = tb_type.Text; watch.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.LastWrite; watch.EnableRaisingEvents = true ;
After setting up the FileSystemWatcher class, you need to handle the event raised by it. The simple way you can do that is by selecting the FileSystemWatecher object and go to Properties -> Events and selecting the event you want to take custom actions when it occurs.
Here is the main window of the application in action:
Project Files File Watcher