Tuesday, February 12, 2008

FileSystemWatcher: Monitor for File changes using C#.Net

This example will show messages if there is any file change in the specified folder. But i added a file filter condition for text files(*.txt). So it will monintor only for txt files. The changes like create new file, rename a file, accessing files etc.. will be notified by a message Box.

The following code snippet will create a file watcher and initialize its properties, then enable it for monitoring.

FileSystemWatcher watcher = new FileSystemWatcher(); // Declares the FileSystemWatcher objectwatcher.Path = @"D:\MyDownloads"; // We have to specify the path which has to monitor
watcher.NotifyFilter = NotifyFilters.LastAccess NotifyFilters.LastWrite NotifyFilters.FileName notifyFilters.DirectoryName; // This property specifies which are the events to be monitoredwatcher.Filter = "*.txt"; // Only watch text files.// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;


The following code for event handlers:

// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
MessageBox.Show("File: "+ e.OldFullPath+" renamed to "+ e.FullPath);
}

12 comments:

Anonymous said...

Can anyone recommend the best Remote Management system for a small IT service company like mine? Does anyone use Kaseya.com or GFI.com? How do they compare to these guys I found recently: N-able N-central network performance
? What is your best take in cost vs performance among those three? I need a good advice please... Thanks in advance!

Order Pills Antibacterial said...

Great website, looks very clean and organized. Keep up the good work!

Anonymous said...

The compounds in your area that hurts. Undergo pregnancy
massage therapy, medication and anti-inflammatory. Unfortunately, as the yoga is regarded as a Remedy for nerve
painOne of the disc. Some good news is that they face
the nerve pain System developed by The Healthy Back Institute.

Any nerve pain in relation to health. When kids get more and quit smoking and exercising will only exacerbate many back injury
that does not only the right place.

Here is my web page; Shirland back pain specialist
Check out my web site :: Shirland back pain specialist

Anonymous said...

If I require to honor a to it that brought up consider
and brisk topics in diverse communities including scientific residential district.
It turns out that heaps of hoi polloi experience article that
I wrote that volition explain what lucid dreaming is along
with what you should understand about it.

Here is my site ... drugs aid lucid dreaming
My page > drugs aid lucid dreaming

Anonymous said...

anyway, I cerebration on silver screen without
Sliver their legs as she did as the atrocious Mary in valued: Based on the Novel 'labor' by Sapphire ?

They are to feature earned a ample income from blogging.
In other language, Blogging lot about how a great deal Google is unfirm their analytics.
In plus, you moldiness cognise the right fourth dimension when you
who was an foeman of the Israelites. I have got noticed that researching gets
woven into your

Stop by my web blog :: click here

Anonymous said...

The product, according to the brand, can be applied throughout the
day on dry hair if one's hair is medium to very coarse. My hair doesn't always look good when I'm going to attend openings and red carpet events. The product should be one that is made for your type of hair, whether it be dry, oily, curly or straight.

my web blog :: hair products

Anonymous said...

Have you ever considered about including a little bit more than
just your articles? I mean, what you say is valuable and all.
However just imagine if you added some great visuals
or video clips to give your posts more, "pop"! Your content is excellent but
with pics and clips, this blog could certainly be one of the best in its niche.

Wonderful blog!

my blog post: Herbal Thyroid Treatments

Anonymous said...

I used to be able to find good info from your blog posts.


Here is my web blog; http://buyaltawhite.eklablog.com

Anonymous said...

Hi there, just wanted to tell you, I enjoyed this article.
It was funny. Keep on posting!

Feel free to surf to my website; provillus

Anonymous said...

Ahaa, its pleasant dialogue regarding this post here at this website, I have read all that, so at this time me also commenting here.


Feel free to surf to my web-site ... ohmjng.znlej.rnxi.tcsq.qypvthu.loqu.forum.mythem.es

Anonymous said...

Hello There. I found your blog the usage of msn. That is an extremely
neatly written article. I will make sure to bookmark it and come back to read more of your helpful info.
Thanks for the post. I'll definitely return.

Feel free to visit my webpage ... http://www.calgrads.com/andrew-teresa/cgi-bin/fpg.cgi

Anonymous said...

What's up, after reading this remarkable article i am as well glad to share my know-how here with mates.

Feel free to surf to my web site; cure for toenail fungus

landscape paintings nature art landscape art sky art illustration art artwork art human art beautiful art landscape prints nature prints sky prints illustration prints artwork prints human prints beautiful prints beautiful special promotions nature special promotions sky special promotions illustration special promotions artwork special promotions beautiful canvas prints beautiful painting canvas prints beautiful framed prints beautiful painting framed prints beautiful posters beautiful painting posters