MyOddWeb.DirectoryWatcher 0.2.0

Requires NuGet 2.12 or higher.

dotnet add package MyOddWeb.DirectoryWatcher --version 0.2.0
                    
NuGet\Install-Package MyOddWeb.DirectoryWatcher -Version 0.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MyOddWeb.DirectoryWatcher" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MyOddWeb.DirectoryWatcher" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="MyOddWeb.DirectoryWatcher" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MyOddWeb.DirectoryWatcher --version 0.2.0
                    
#r "nuget: MyOddWeb.DirectoryWatcher, 0.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MyOddWeb.DirectoryWatcher@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MyOddWeb.DirectoryWatcher&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=MyOddWeb.DirectoryWatcher&version=0.2.0
                    
Install as a Cake Tool

Myoddweb.Directorywatcher Release

A fast and reliable File/Directory watcher for c#/c++ to replace the current .NET FileSystemWatcher class.

What it does

  • Reliable monitoring of
    • Renamed files/directories
    • Deleted files/directories
    • Created files/directories
    • Modified (touched) files/directories
  • All exceptions are passed back to the caller.
  • Non-blocking delegates, if one function takes a long time ... we don't all have to suffer.
  • The public interfaces are platform-agnostic, so other backends could be added later, (see Requirements for what is actually implemented today).
  • No buffer limitations, (well there are, but we play nicely).
  • Try and remove duplicates, (where possible).
  • Deleted (then re-created) folders are re-monitored.
  • Watcher statistics

What it doesn't do

  • Bring me coffee.

Requirements

  • Windows only, for now. The native watcher uses the Win32 ReadDirectoryChangesW API. There is currently no macOS/Linux backend.
  • The managed library targets .NET Framework 4.6.2+, .NET Standard 2.0+ and .NET 8.0+.

Installing

Nuget

NuGet Status NuGet Count

Package manager

Install-Package MyOddWeb.DirectoryWatcher

CLI
.NET

dotnet add package MyOddWeb.DirectoryWatcher

Paket

paket add MyOddWeb.DirectoryWatcher

Use case

My needs were to, reliably, monitor entire volumes for created/deleted/renamed files. I don't really care for pattern matching.

The issue(s) with FileSystemWatcher

The current version of File Watcher is great, but it does have a couple of issues.

  • There is a buffer limitation, (in the API itself), and a badly written application can 'block' or 'miss' certain notifications.
  • Duplicates are often sent, (when a file is updated 3 times between calls, we only need to know about it once).
  • Certain exceptions cause the entire app to close.
  • UNC/Unix files are not supported, (in fact it causes FileSystemWatcher to take your system down).
  • Does not handle large volumes nicely.

Examples

Simple Watch

Add all the directories we want to 'observe'

    using( var watch = new Watcher() )
    {
      watch.Add(new Request("c:\\", true));
      watch.Add(new Request("d:\\foo\\bar\\", true));
      watch.Add(new Request("y:\\", true));

      // do something amazing with the data
      watch.OnAddedAsync += async (f, t) =>
      {
        // ..
      };

      // start watching
      watch.Start();

      // add some more
      watch.Add(new Request("z:\\", false));

      // optional stop in this case
      watch.Stop();
    }

You can start watching at any point

    // create Watcher
    var watch = new Watcher();

    // Add a request.
    watch.Add(new Request("y:\\", true));

    // start watching
    watch.Start();

    // add some more
    watch.Add(new Request("z:\\", false));

Get notifications in case a file is created.

    watch.OnAddedAsync += async (f, t) =>
    {
      Console.ForegroundColor = ConsoleColor.Green;
      Console.WriteLine(
        $"[{f.DateTimeUtc.Hour}:{f.DateTimeUtc.Minute}:{f.DateTimeUtc.Second}]:{f.FileSystemInfo}");
      Console.ResetColor();
    };

We get given the file that was added as well as a cancellation token

You can also check, at any time, whether all of your requests have actually started monitoring.

    if (watch.Ready())
    {
      // every request that was added has started monitoring.
    }

And when we are done stop it ...

    watch.Stop();

Or Dispose of it

    watch.Dispose();

Your own 'Watcher' interface

You can create your own watcher interface

public class Watcher : IWatcher3
{
  // Implement IWatcher3
}

Watched Events

When a file event is raised we send a IFileSystemEvent event.

    /// <summary>
    /// The file system event.
    /// </summary>
    FileSystemInfo FileSystemInfo { get; }

    /// <summary>
    ///  Gets the full path of the directory or file.
    /// </summary>
    /// <returns>A string containing the full path.</returns>
    string FullName { get; }

    /// <summary>
    ///     For files, gets the name of the file. For directories, gets the name of the last
    ///     directory in the hierarchy if a hierarchy exists. Otherwise, the Name property
    ///     gets the name of the directory.
    /// </summary>
    /// <returns>A string that is the name of the parent directory, the name of the last directory
    ///     in the hierarchy, or the name of a file, including the file name extension.
    /// </returns>
    string Name { get; }

    /// <summary>
    /// The Action
    ///  Added
    ///  Removed
    ///  Touched
    ///  Renamed
    /// </summary>
    EventAction Action { get; }

    /// <summary>
    /// An error code related to the event, (if any)
    /// </summary>
    EventError Error { get; }

    /// <summary>
    /// The UTC date time of the event.
    /// </summary>
    DateTime DateTimeUtc { get; }

    /// <summary>
    /// Boolean if the update is a file or a directory.
    /// </summary>
    bool IsFile { get; }

    /// <summary>
    /// Return if the event is a certain action
    /// (same as Action == action)
    /// </summary>
    /// <param name="action"></param>
    /// <returns></returns>
    bool Is(EventAction action );
Renamed events

OnRenamedAsync gives you an IRenamedFileSystemEvent instead, (it extends IFileSystemEvent above), with the file/directory's previous name as well as its new one.

    /// <summary>
    /// The file system info, before the rename.
    /// </summary>
    FileSystemInfo PreviousFileSystemInfo { get; }

    /// <summary>
    /// The full path of the file/directory before the rename.
    /// </summary>
    string PreviousFullName { get; }

    /// <summary>
    /// The name of the file/directory before the rename.
    /// </summary>
    string PreviousName { get; }

Statistics

You can get statistics at various intervals for the events being watched.

All you need to do is add Rates to your watchers. Rates takes the events rate first and the statistics rate second, (both in milliseconds); either one left at 0, the default, turns that particular feed off.

    using( var watch = new Watcher() )
    {
      // watch the folder, publishing statistics every 10000 ms
      // while leaving the events rate at its default.
      watch.Add(new Request("c:\\", true, new Rates(50, 10000 )));

      // do something amazing with the statistics
      // the value is an `IStatistics` with a cancellation token
      watch.OnStatisticsAsync += async (s, t) =>
      {
        // ..
      };

      // start watching
      watch.Start();

      // ... do some clever stuff.

      // optional stop in this case
      watch.Stop();
    }

IStatistics gives you:

    /// <summary>
    /// The id of the request these statistics are for.
    /// </summary>
    long Id { get; }

    /// <summary>
    /// The elapsed time, (in ms), since the last statistics message.
    /// </summary>
    double ElapsedTime { get; }

    /// <summary>
    /// The total number of events since the last statistics message.
    /// </summary>
    long NumberOfEvents { get; }

Logger

You can watch for certain events

  • Unknown = 0, should never happen
  • Information = 1, nothing important, maybe something worth noting
  • Warning = 2, something happened, but we managed to recover from it
  • Error = 3, something broke, messages were probably lost.
  • Panic = 4, something really bad happened, the process probably died.
  • Debug = 100, debug-only messages, should not appear in release builds
    using( var watch = new Watcher() )
    {
      watch.Add(new Request("c:\\", true ));

      // do something amazing with the message
      // the value is an `ILoggerEvent` with a cancellation token
      watch.OnLoggerAsync += async (e, t) =>
      {
        // ..
      };

      // start watching
      watch.Start();

      // ... do some clever stuff.

      // optional stop in this case
      watch.Stop();
    }

ILoggerEvent gives you:

    /// <summary>
    /// The id of the request this message relates to.
    /// </summary>
    long Id { get; }

    /// <summary>
    /// The message log level, (see the list above).
    /// </summary>
    LogLevel LogLevel { get; }

    /// <summary>
    /// The actual message.
    /// </summary>
    string Message { get; }
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 47 8/30/2026
0.1.9 70,202 8/2/2020

Version 0.2.0:
- Updated toolset to Visual Studio 2022 (v143)
- Added .NET 8.0 support (dropped deprecated .NET 4.5.2 and .NET Core 3.0)
- Fixed issue #20: full folder copy/paste event detection
- Fixed race conditions during recursive monitor startup and worker pool teardown
- Updated test frameworks to NUnit 4.6.1 and Google Test 1.18.0