FileSystemWatcher accuracy

Posted on 2005-05-04
Last Modified: 2008-02-01
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?
Question by:Neal Hartman
    1 Comment
    LVL 28

    Accepted Solution

    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 ---

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now