Click here to Skip to main content
15,617,231 members
Articles / Desktop Programming / Windows Forms
Posted 30 May 2008


108 bookmarked

C#: Application to Watch a File or Directory using FileSystem Watcher

Rate me:
Please Sign up or sign in to vote.
4.75/5 (64 votes)
30 May 2008CPOL3 min read
This article describesthe usage of the FileSystemWatcher object to watch changes to a file or a directory in C#



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.


The 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

A FileSystemWatcher instance can be created as follows using the new keyword:
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 | 
                         NotifyFilters.LastWrite | 
                         NotifyFilters.FileName | 

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 e.ChangeType

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 uploaded on 05/31/2008
Version uploaded on 06/01/2008


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Program Manager Wipro Technologies
India India
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

Comments and Discussions

QuestionHow to implement the same application in MFC Pin
Member 927486411-Sep-12 18:49
Member 927486411-Sep-12 18:49 
Questionhow to get file changes Pin
sher_ali15-May-12 8:30
sher_ali15-May-12 8:30 
GeneralMy vote of 5 Pin
DFriday1328-Feb-12 10:18
DFriday1328-Feb-12 10:18 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey15-Feb-12 23:24
professionalManoj Kumar Choubey15-Feb-12 23:24 
QuestionDirectory is not showing Pin
Ankita Mali6-Jan-12 18:14
Ankita Mali6-Jan-12 18:14 
GeneralMy vote of 1 Pin
Werner van Deventer8-Oct-11 5:43
Werner van Deventer8-Oct-11 5:43 
Question[My vote of 1] ARE YOU KIDDING ME?! Pin
Seishin#6-Oct-11 22:19
Seishin#6-Oct-11 22:19 
AnswerRe: [My vote of 1] ARE YOU KIDDING ME?! Pin
Rohit Dubey from Hyderabad11-Jan-12 13:26
Rohit Dubey from Hyderabad11-Jan-12 13:26 
seriously, 99% of code project 'articles' are along the lines of
This is how you use {xxxx}

var poo = new {xxxx}();

I've just shown you how to use {xxxx}
Thank you.
Gotta learn how to filter code project out of search results
SuggestionRe: [My vote of 1] ARE YOU KIDDING ME?! Pin
Member 821039916-Jun-12 0:55
Member 821039916-Jun-12 0:55 
AnswerRe: [My vote of 1] ARE YOU KIDDING ME?! Pin
Prashant K Singh22-Oct-13 4:26
professionalPrashant K Singh22-Oct-13 4:26 
GeneralRe: [My vote of 1] ARE YOU KIDDING ME?! Pin
James Campbell26-Dec-14 12:23
James Campbell26-Dec-14 12:23 
GeneralGreat application! Pin
San4Ramyam7-Jun-11 22:02
San4Ramyam7-Jun-11 22:02 
GeneralPlease help me Pin
tranquocvietdanang2-Aug-10 22:01
tranquocvietdanang2-Aug-10 22:01 
Questionhow to watching two folders? or more? Pin
yuyejian12-Jun-10 22:36
yuyejian12-Jun-10 22:36 
AnswerRe: how to watching two folders? or more? Pin
PrinuAji22-Jun-11 5:54
PrinuAji22-Jun-11 5:54 
GeneralI am not able to show the results. Pin
ashu_himt23-Mar-10 7:16
ashu_himt23-Mar-10 7:16 
GeneralVote 5/5 Pin
anhduc.bkhn10-Oct-09 0:44
anhduc.bkhn10-Oct-09 0:44 
GeneralMy vote of 1 Pin
Felix Alcala2-Feb-09 12:58
Felix Alcala2-Feb-09 12:58 
QuestionRe: My vote of 1 Pin
Prashant K Singh15-Feb-09 0:53
professionalPrashant K Singh15-Feb-09 0:53 
GeneralRe: My vote of 1 Pin
MrJoe0128-Aug-09 0:10
MrJoe0128-Aug-09 0:10 
SuggestionRe: My vote of 1 Pin
Bo Alexander19-Jul-11 9:16
Bo Alexander19-Jul-11 9:16 
GeneralHandling OnDelete & OnRename Pin
mahone9-Dec-08 4:38
mahone9-Dec-08 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.