FileSystemWatcher accuracy

I have some apps that use FileSystemWatcher to detect new files. And not a lot of new files either. For some reason the FileSystemWatcher does not always detect the new files. Does anyone have similar results and solutions?
Neal HartmanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

works only on ME,NT,2k and XP

Beware of potential problem - when too many events fire in a short time. The FileSytemWatcher component
uses an internal buffer to keep track of file system actions so events can be raised for each one of
them even if the application cannot serve them fast enough. By default the internal buffer is 8kb long
and can store about 160 events; each event takes 16 bytes plus 2 bytes for each character in file name.
Filenames are stored in Unicode characters.
If you anticipate a lot of file activity - you must do one of the following actions to prevent buffer
from overflowing:
1.Increase the size of buffer by setting the InternalBufferSize to a larger value. The size must be multiple
of operating system page size (4kb under W2k)
2.Use the NotifyFilter property to limit the number of change operations that fire the Change event
3.Set IncludeSubdirectories to False if you don't need to monitor the entire directory tree. (Use multiple
  FIleSystemWatcher components to monitor each subfolder)

You can't use Filter property to prevent internal buffer overflow because this propery filters out files
only after the've been added to the buffer. When overflow happens you get the error

 Private Sub fsw_Error(ByVal sender As Object, ByVal e As System.IO.ErrorEventArgs) Handles fsw.Error
        LogMessage("FileSystemWatcher error")
 End Sub

From the "Troubleshooting FileSystemWatcher Components" section of the VS.NET documentation...

--- begin quote ---
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.
--- end quote ---

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.