In this article I describe the usage of the
FileSystemWatcher object provided by VS 2008 (note: This object is same as the one in VS 2005), using C#. The application created here can be used to monitor any file or directory on your system. The generated change list contains notifications for creation, deletion, update or renaming of the file/directory content.
FileSystemWatcher object provided by .Net is a useful way to monitor a file system. Its definition is contained in the
System.IO namespace. This object contains fields to mark which file or directory is to be monitored. Additionally the
FileSystemWatcher object allows you to monitor a certain type of files in a directory using wildcards (eg. *.txt).
Using the code
instance can be created as follows using the
m_Watcher = new System.IO.FileSystemWatcher();
Then we need to assign it a path and a filter to tell the object where to keep looking.
The line below tells the watcher that it has to keep looking at the path entered in the txtFile textBox. The '\\' characters at the end are to make sure that the path has a directory name. Orelse there will be a problem in case the user enters something like "C:" in the text box.
m_Watcher.Path = txtFile.Text + "\\";
Next we need to tell the watcher what all to look at.
The line below tells the watcher what files it is supposed to watch.
m_Watcher.Filter = strFilter;
The value formats of
strFilter and their meanings are as follows:
*.* - Watch all files in the Path
*.ext - Watch files with the extension ext
name.ext - Watch a particular file name.ext
Note that the file name.ext may not exist when the watcher begins watching. But as the file is created/moved to the Path directory, the watcher starts watching the file thereon.
Next we need to tell the watcher what to look for.
The following line does exactly that using various flags, each one describing a certain type of attribute of the file system.
m_Watcher.NotifyFilter = NotifyFilters.LastAccess |
We can also tell the watcher to watch for changes to the sub-folders of the directory we specify in the Path by doing the following.
m_Watcher.IncludeSubdirectories = true;
Next we need to describe what needs to be done when one of these attributes gets altered.
This is done by assigning different event handlers to different activities. In the present application we have used the same event handler for creation, change and deletion. This is done because we just need to log these changes the same way, and the change name is fetched from the argument in the event handler.
m_Watcher.Changed += new FileSystemEventHandler(OnChanged);
m_Watcher.Created += new FileSystemEventHandler(OnChanged);
m_Watcher.Deleted += new FileSystemEventHandler(OnChanged);
m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);
Note that Renamed activity has a different handler. This is because it has a different signature for its event handler. The signatures of both the handlers are as follows.
void OnChanged(object sender, FileSystemEventArgs e)
void OnRenamed(object sender, RenamedEventArgs e)
The change type in both cases can be fetched from the
e argument as
Lastly, we need to tell the watcher to do its job - Watch It!!!
This is done by enabling the watcher to raise events. This is done by the following line.
m_Watcher.EnableRaisingEvents = true;
Once this is done, the watcher keeps watching the assigned file/files or folders and appropriate events will be raised for their respective activities.
Points of Interest
Once the FileSystemWatcher is set to watch a file or folder, it will keep monitoring it till the end of the application. Setting the
m_Watcher value to
null will not stop it from monitoring. To stop the watcher while the application is still running, we need to stop it from raising events. This is done as follows.
m_Watcher.EnableRaisingEvents = false;
The current version of this application does not work for networked drives or folders shared over the network. I will post the code with this functionality in due time.
Version 188.8.131.52 uploaded on 05/31/2008
Version 184.108.40.206 uploaded on 06/01/2008
I have a Bachelor's Degree in Electrical Engineering, class of 2004 and has been interested in software development since second grade (you read it right)
I have built and led highly efficient and performant teams from ground up and have been able a part of several Digitization and Cloud Transformation efforts for large enterprises.
Apart from software building, I also hold a keen interest in Photography, Travelling and tinkering with Arduino and Raspberry Pi