Click here to Skip to main content
12,898,569 members (73,678 online)
Click here to Skip to main content
Add your own
alternative version


68 bookmarked
Posted 20 Apr 2002

A .NET File System Watcher

, 20 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
A utility that monitors a selected directory for changes

Sample Image - FSW1.jpg


FSWatcher is a small utility that I wrote for a Distributed Objects class. No, FSWatcher doesn't know how to remote or anything like that! Instead, it just looks on the local drive space for the selected file and waits. Anytime anything changes, FSWatcher notifies the user by checking the box next to the item (if it exists!) and updating the label at the bottom of the dialog box.

This was a pretty simple project, once I knew what to look for. I found everything in the Visual Studio .NET help files. I don't know about you, but that's a first for me!

Sample Image

The meat of the code is here, where the filters and delegates are set up. The code is actually so easy that I won't insult your intelligence by stepping you through it. If you don't understand it, then use it as a guide and try to figure it out.
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = textBox1.Text;
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | 
                   NotifyFilters.DirectoryName | NotifyFilters.FileName;
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnCreated);
fsw.Deleted += new FileSystemEventHandler(OnChanged);
fsw.Renamed += new RenamedEventHandler(OnRenamed);
fsw.EnableRaisingEvents = true;
I had a lot of fun with this... I hope you do, too.


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


About the Author

Software Developer (Senior)
United States United States
Tony has a real job as a software Engineer. His interest in C# started with a homework assignment in a Distributed Objects course. Tony lives in New York state but hails from LA. At home on Solaris or Windows, he prefers Visual Development. C# and WPF rock!

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
anprox4-Sep-11 22:58
memberanprox4-Sep-11 22:58 
GeneralCross-thread operation not valid: Control 'label2' accessed from a thread other than the thread it was created on Pin
NileshUpadhyay4-Nov-09 20:36
memberNileshUpadhyay4-Nov-09 20:36 
GeneralRe: Cross-thread operation not valid: Control 'label2' accessed from a thread other than the thread it was created on Pin
MCY18-Jan-11 1:08
memberMCY18-Jan-11 1:08 
GeneralHi... Pin
Nehal Pandya MCA12-Jun-09 19:06
memberNehal Pandya MCA12-Jun-09 19:06 
QuestionHow to determine drive types Pin
liuhoihing17-Nov-03 19:15
memberliuhoihing17-Nov-03 19:15 
GeneralNot working ! Pin
Anonymous29-Jun-03 7:35
sussAnonymous29-Jun-03 7:35 
GeneralRe: Not working ! Pin
leppie29-Jun-03 7:47
memberleppie29-Jun-03 7:47 
GeneralRe: Not working ! Pin
mwdiablo1-Apr-04 3:20
membermwdiablo1-Apr-04 3:20 
GeneralRe: Not working ! Pin
kadnan26-Sep-04 22:35
memberkadnan26-Sep-04 22:35 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 21 Feb 2014
Article Copyright 2002 by toneware
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid