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?
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 ---
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.