Click here to Skip to main content
Click here to Skip to main content

FileSystemWatcher - Pure Chaos (Part 2 of 2)

By , 17 Dec 2010
 

Introduction

In Part 1 of this article series, I created a class that sends a separate event for each NotifyFilter item in the DotNet FileSystemWather object.

View Part 1 of this article series. This is where you should go to get the sample application.

In this article, I'll discuss the ramifications of using this class, and some quirks regarding the functionality of the FileSystemWatcher object. This article is more a series of screen shots and relevant discussion rather than an examination of code, so if you're not inclined to read this, don't feel too bad about it. However, I would recommend that you do read this one, because it reveals what I consider to be "good-to-know" info.

We won't be exercising the folder existence code in this article, and instead will be focusing on the standard events that are spewed out by the DotNet FileSystemWatcher object.

Note: I'm using Windows7 Ultimate (64-bit). I don't know if you'll see different results on older (or newer) versions of Windows. I see no reason why you should, but hey, who knows?

Before We Start

First, I assume that you downloaded the source code/ demo application from Part 1. If not, and if you'd like to sing along, please download that code now. Since the release binary is included with the project, you may simply run it (there's no need to fire up the IDE and compile it).

Some Interesting (For Me) Discoveries

When we fire up the demo application, we are greeted with the following form.

The TextBox is automatically populated with a folder that exists on my system. Your first action should be to change this value. You can use the browse button to find an appropriate folder. You may want to change the hard-wired path value to something that reflects your own drive\folder hierarchy so that the default folder is more appropriate for you, but you'll have to change the code (in the form) and recompile if you want to do this. Remember, it's just a demo app and not all of the expected bells and whistles, such as saving/retrieving settings, have been implemented.

Creating a File

There are several ways to create a new file in a folder. As expected, simply right-clicking in the folder and selecting "New Text Document" from the context menu results in a single event.

part2_image_02.jpg

However, this is what happens when you create a new file from Notepad.

part2_image_03.jpg

Holy Crap! That's a LOT of events for simply saving a new file from Notepad. I checked the debug trace and that's apparently the order in which the events are really fired. In this case the file was only 7K, so the events appeared to all fire at pretty much the same time. You would think that would explain why the time of the event appears to be the same, but check this out:

part2_image_04.jpg

This output was generate when I tried to save a 7mb file from Notepad. In real time, it took 14 seconds to get the last event generated by saving such a large file. And if you think Notepad is messed up, check out the results of creating a new file from Microsoft Word 2007:

part2_image_05.jpg

Man, I never knew there was so much going on behind the scenes in Microsoft Word. Let's check out saving a new file from PaintShop Pro...

part2_image_06.jpg

Granted, that's a little better, but still, notice the repeated Created event (a common theme). As if that's not weird enough, here's what happens when you copy a file from a folder on the same hard drive into our watched folder.

part2_image_07.jpg

I don't see any Created events at all, despite the fact that the file was "created" in the folder. Here's what happens when you copy a file from a different hard drive.

part2_image_08.jpg

Finally, I downloaded a file with FireFox into the watched folder, and got these results.

part2_image_09.jpg

I don't know about you, but I'm tired. :)

It Boggles The Mind

Personally, I think the FileSystemWatcher object is - for lack of a better term - just plain wacky. The bizarre aspect of this is that it's not really the fault of the object, because it's merely reporting what's happening in the folder.

There's no possible way to predict what someone else's needs will be with regards to using the FileSystemWatcher object. As you can see, results vary from one extreme to the other. I suppose what we've learned here is that how you handle the object's events depends COMPLETELY on how (and by what mechanism/application) you expect the files to be placed in the watched folder. In light of this realization, you could make use of this demo application so that you can investigate your own requirements and design a cohesive plan for handling those events in whatever manner you might choose. The upside is that now you don't have to perform nearly as many reactive changes in the code in order to handle all of your expected file types (and their source applications/mechanisms).

Finally, you can use this sample application to examine your own applications' file handling processes. You might be surprised when you see exactly what is happening in your own applications.

History

12/18/2010 - Fixed some wording and a couple of misspellings.

02/14/2010 - Original version.

License

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

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
Member
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNeed assistance with filesystemwatcher issuememberSairam Kannan10 Apr '13 - 2:23 
GeneralMy vote of 5memberPrasad Khandekar28 Mar '13 - 7:58 
GeneralGood To KnowmemberBenjano8 Nov '12 - 7:47 
GeneralMy vote of 5memberWegierski25 Oct '12 - 19:57 
Question"Created" EventmemberTheAlas27 Jul '12 - 4:34 
QuestionExcellent articlememberMike Hankey19 Jun '12 - 1:19 
QuestionGreat InformationmemberDirkster991 Jun '12 - 3:50 
GeneralMy vote of 5memberAkram El Assas29 Feb '12 - 0:22 
QuestionLibraries and networkmembergxdata11 Nov '11 - 19:51 
AnswerRe: Libraries and networkmvpJohn Simmons / outlaw programmer12 Nov '11 - 0:44 
GeneralMy vote of 5memberFilip D'haene7 Sep '11 - 11:14 
GeneralMy vote of 5memberNagy Vilmos11 May '11 - 1:36 
GeneralMy vote of 5memberSAKryukov3 Feb '11 - 20:52 
GeneralMy vote of 5memberayalasolutions14 Jan '11 - 11:50 
GeneralMy vote of 5memberMember 432084424 Dec '10 - 13:47 
GeneralMy vote of 5memberpolczym23 Nov '10 - 1:04 
GeneralMy vote of 5memberbhiller30 Sep '10 - 23:06 
GeneralMy vote of 5memberCIDev14 Sep '10 - 14:33 
Generalnice onememberSteppenwolfe7 Jul '10 - 22:47 
GeneralRe: nice onememberJohn Simmons / outlaw programmer14 Jul '10 - 1:46 
GeneralMy vote of 5memberkarabax7 Jul '10 - 13:45 
GeneralMy vote of 5memberBryanWilkins7 Jul '10 - 4:04 
QuestionWin7 Libraries?memberDoncp17 Mar '10 - 18:33 
AnswerRe: Win7 Libraries?memberJohn Simmons / outlaw programmer18 Mar '10 - 2:30 
Questionother events..memberjosephlawrencecarpio16 Feb '10 - 1:53 
AnswerRe: other events..memberJohn Simmons / outlaw programmer16 Feb '10 - 4:16 
Generalsome similar code that goes a bit furthermemberVasian Cepa15 Feb '10 - 23:25 
GeneralRe: some similar code that goes a bit further [modified]memberJohn Simmons / outlaw programmer16 Feb '10 - 0:59 
GeneralVery InterestingmemberRama Krishna / Sensible Programmer15 Feb '10 - 9:29 
GeneralNice seriesmemberMarcelo Ricardo de Oliveira15 Feb '10 - 1:10 
GeneralNicememberAngsuman Chakraborty14 Feb '10 - 22:26 
GeneralHighly informative ArticlememberYogi Yang14 Feb '10 - 19:19 
GeneralNicememberPandey Vijay Kumar S.14 Feb '10 - 16:48 
GeneralNice articlememberAbhinav S14 Feb '10 - 10:42 
GeneralRe: Nice articlememberJohn Simmons / outlaw programmer14 Feb '10 - 11:18 
GeneralGreatmvpLuc Pattyn14 Feb '10 - 10:07 
GeneralRe: GreatmemberJohn Simmons / outlaw programmer14 Feb '10 - 10:18 
GeneralRe: GreatmvpLuc Pattyn14 Feb '10 - 10:24 
GeneralRe: Greatmemberjberke16 Feb '10 - 4:54 
AnswerRe: GreatmvpLuc Pattyn16 Feb '10 - 5:00 
GeneralRe: Greatmemberjberke16 Feb '10 - 5:06 
GeneralRe: GreatmemberJohn Simmons / outlaw programmer16 Feb '10 - 7:11 
GeneralRe: Greatmemberdchrno22 Feb '10 - 11:16 
GeneralRe: GreatmemberRose Epinglee23 Sep '10 - 15:46 
GeneralRe: GreatmvpLuc Pattyn14 Feb '10 - 10:33 
GeneralRe: GreatmemberJohn Simmons / outlaw programmer14 Feb '10 - 11:20 
GeneralRe: GreatmemberJohn Simmons / outlaw programmer14 Feb '10 - 11:22 
GeneralRe: GreatmvpLuc Pattyn14 Feb '10 - 11:38 
GeneralRe: GreatmemberNguyenTrucMinh23 Sep '10 - 15:44 
GeneralExcellentmembersam.hill14 Feb '10 - 9:38 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 18 Dec 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid