Lean Solutions & Rich User Experiences

FileWatcher, FilePoller Components

[Updated May 4, 2015]

Adds robust NTFS file detection to your Applications:

  • Instantly detects existing, new and renamed files.
  • Monitors directory availabililty.
  • FileWatcher is a robust wrapper around IO.FileSystemWatcher
    hooking into NTFS events.
  • FilePoller periodically polls for new files.

For a detailed discussion about watching the Windows file system see my blog post Tamed FileSystemWatcher.


Imports Meinl.LeanWork.FileSystem

Sub Main()
  Dim cts As New CancellationTokenSource
  Dim poller As New FilePoller("d:\temp", "*.*",
  poller.StartProcessing(AddressOf ProcessFile, cts.Token)
End Sub

Sub ProcessFile(fileInfo As FileChangedInfo,
                         CT As CancellationToken)
End Sub

License: New BSD License


We recommend using FileWatcher over FilePoller because it offers both instant and robust notifications with minimal resource consumption.
FilePoller and FileWatcher automatically recover from commen exceptions like network name is no longer available. You may observe and handle these exeptions yourself using the Error event and setting e.Handled.
FileWatcher exposes detected FileInfos in a Concurrent.BlockingCollection which you can easily consume using GetConsumingEnumerable without a while-loop:

Sub ProcessFilesWait(detectedFiles As BlockingCollection(Of FileChangedInfo),
                     CT As CancellationToken)
    'Waits for new items if BlockingCollection is empty
    ' until cancelled
    For Each fileChangedInfo
              In detectedFiles.GetConsumingEnumerable(CT)
      ProcessFile(fileChangedInfo, CT)
  Catch ex As OperationCanceledException
    _trace.Debug("Obeying cancel request")
  End Try
End Sub
FilePoller executes an Action you pass to in its StartProcessing Method:

Task.Run(Sub() poller.StartProcessing(
               AddressOf ProcessFile, CT),

When using asynch processing you should rename the detected file to a different directory before the asynch operation to prevent files from being detected again in the next poll.


The standard IO.FileSystemWatcher:
  • Does not detect files that existed before .EnableRaisngEvents =True.
  • Does automatically handle renames of its watch path.
  • Does detect network disruptions
    but does not automatically recover from them.
  • Reports Exceptions via its Error event
    not via raising Exceptions!
FileWatcher and FilePoller  monitor watch path availability and implement cooperative cancellation via a CancellationToken.

FileWatcher uses a FileSystemWatcher to detect configurable watch path changes. In addition it detects files that existsed before the FileSystemWatcher was started. For added robustness it monitors the watch path using a Timer with a configurable interval (typically some minutes) and automatically restarts itself on recoverable exceptions. It honors a CancellationToken.

FilePoller enumerates over the watch path files and calls the passed Action until if finds no more files. It loops with a configurable  delay (typically some seconds, Windows minimum approx. 15ms). Enumerating and delay honor a CancellationToken.