Pages

Monday, July 9, 2012

File System Watcher class in .Net (C#)

Sometime you might need to monitor changes like files added, deleted, renamed for particular folder. To do so, .net provides
FileSystemWatcher class to monitor such kind of activity for particular folder
or file.

FileSystemWatcher class provides some events which are fired
when some action done to the folderlike file created, deleted, renamed
etc. You can also specify filter to file system watcher if you want to watch only text
file or music files etc. It also provides an option to include subdirectories to
watch or monitor.

As per above code, I have chosen Test folder to monitor file
system changes. After running this application, if I add, delete, rename any
text file to this folder, it will notify FileSystemEvent to raise respective
event and the code written in respective event handler will execute.