Skip to content

Multiple Events Fired for File System Watcher Event

My FileSystemWatcher class was raising multiple events when hooked into the Changed and Created events for monitoring files today.

I found out why! To debug and trigger these events I was using one of my favourite text editors, EditPlus which performed a save of my file in multiple segments, consequently raising these events more than once!

Check out the following quote below for more information on this:

Multiple Created Events Generated for a Single Action

You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. This is because Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up.

Note: Notepad may also cause other interesting event generations. For example, if you use the ChangeEventFilter to specify that you want to watch only for attribute changes, and then you write to a file in the directory you are watching using Notepad, you will raise an event . This is because Notepad updates the Archived attribute for the file during this operation.

So watch out, this could also be the case when Anti Virus decides to intervene too.

VN:F [1.9.1_1087]
Rating: 5.0/5 (2 votes cast)
VN:F [1.9.1_1087]
Rating: 0 (from 0 votes)
Multiple Events Fired for File System Watcher Event, 5.0 out of 5 based on 2 ratings
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

5 Comments

  1. How odd. I experienced the exact same thing today with a file watcher I was testing. I use UltraEdit, and I was getting 3 events when I expected one. Sounds like the same problem. How timely :)

    VA:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 29-May-09 at 11:19 am | Permalink
  2. Graham O'Neale

    Totally weird.
    It’s amazing what happens around you at the same time in the world. :)

    VA:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 29-May-09 at 2:20 pm | Permalink
  3. Vick

    Is there any work around this problem. I am watching a directory and if some body copied a excel file to it, I am loading that data to DB. Multiple event generation is screwing up the whole thing…. :sad:

    VA:F [1.9.1_1087]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 11-Jun-09 at 3:42 am | Permalink
  4. Shibanee

    Could you pl explain how to avoid this multiple events ?

    VA:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 30-Jun-09 at 4:27 am | Permalink
  5. Ahammed

    try
    {
    if (e.ChangeType==WatcherChangeTypes.Changed)
    {
    using (StreamWriter sw = new StreamWriter(@”C:\WINDOWS\Temp\log.txt”, true))
    {
    string message = “File ” + e.FullPath + ” Has been ” + e.ChangeType + ” on ” + System.DateTime.Now;
    sw.WriteLine(message);
    watcher.EnableRaisingEvents = false;
    }
    }

    }

    hope this would help you guys to solve the issue

    VA:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 05-Jul-09 at 9:41 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*
My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...