Monitoring a Folder using FileSystemWatcher class in .Net

FileSystemWatcher class provided in .Net is used for monitoring the activities in a folder or sub-folders. You can watch the folder for specific types of files or all the types of files. You can watch for changes like creating a file, deleting a file, changing a file content etc. although you can create such applications in VB6, it is a tedious task since you have to write a lot of code for achieving that task. But in .Net this task can be done very easily by using the class called the FileSystemWatcher class.

The namespaces that you have to import for using this class are System.IO and System.Diagnostics. To monitor a folder for any activities all you have to do is to create an instance of the FileSystemWatcher class and set the necessary properties and invoke methods. You have to create handlers for the FileSystem events. With all of these done you are ready to watch the activities on a particular folder in your system. You can also watch for activities on remote folders using this FileSystemWatcher class.

Creating an instance of FileSystemWatcher class is just like creating an instance for other classes. The code for creating an instance would look like,

FileSystemWatcher myFolderWatcher = new FileSystemWatcher();

The properties for the FileSystemWatcher class that needs to be set are the Path and the Filter property. The path property sets the path of the folder that is to be watched. The filter property sets the filter for the types of files to be watched. For example if you want to watch for the activities of the word documents in a folder, you can set the filter property to “*.doc”.

It is possible to pass the properties and the filters even while you create an instance of the FileSystemWatcher class. If you are passing these properties while you create an instance, the code will look like,

FileSystemWatcher myFolderWatcher = new FileSystemWatcher(“d:\\”, “*.doc”);

The properties that are usually used in this class are the Path, EnableRaisingEvents, Filter, IncludeSubdirectories, InternalBufferSize, and NotifyFilter. Setting the IncludeSubdirectories enables you to watch for changes in the subdirectories of the path set. The NotifyFilter property gets or sets the type of changes to look for in the folders/subfolders.

The NotifyFilter property can be set to any of the NotifyFilters enumeration values. The enumeration values that are of interest are, Attributes, CreationTime, DirectoryName, FileName, LastAccess, LastWrite, Security, and Size.

The events for which we usually write code are the Changed, Created, Deleted, Error, and Renamed. The Changed event is raised when a file or directory in the path specified changes. The Created event occurs when file is created. The Deleted event is raised when a file or directory is deleted. The Error event is raised when there is an internal buffer overflow. The Renamed event is raised when a file or directory is renamed.

Some of the protected methods for the FileSystemWatcher class are OnChanged, OnCreated, OnDeleted, OnError, and OnRenamed. There is no need for explanation on what these methods do. The name itself indicates that.

A simple example of a code snippet that is used to track changes to the documents in a folder is given below.

Imports System.IO
Imports System.Diagnostics
myFolderWatcher = New System.IO.FileSystemWatcher()

myFolderWatcher.Path = "......give path to the folder here...."

myFolderWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
myFolderWatcher.NotifyFilter = myFolderWatcher.NotifyFilter Or _ IO.NotifyFilters.FileName
myFolderWatcher.NotifyFilter = myFolderWatcher.NotifyFilter Or _ IO.NotifyFilters.Attributes

AddHandler myFolderWatcher.Changed, AddressOf ActivityMesg

myFolderWatcher.EnableRaisingEvents = True


Private Sub ActivityMesg(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
txtActivityMesg.Text &= "File " & e.FullPath & " has been modified" & vbCrLf
End If
End Sub

In the above code we have first imported the namespaces required and then created an object of FileSystemWatcher class. We have set the path of the folder to be watched for. Set the NotifyFilter property to one of the enumeration values of the enumerator NotifyFilters. Set the FileSystemWatcher object’s EnableRaisingEvents to True to start tracking the events.

Add a handler for the Changed event. With this the application would start tracking the changes in the folder mentioned. You can add other events also in the same program and use other NotifyFilters enumeration values to make the program fully functional.

Thus we find that the FileSystemWatcher class is very easy to use to track the activities of a particular folder in your computer or network.


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.