G2Development.FileWatcher 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package G2Development.FileWatcher --version 1.0.1
NuGet\Install-Package G2Development.FileWatcher -Version 1.0.1
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="G2Development.FileWatcher" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add G2Development.FileWatcher --version 1.0.1
#r "nuget: G2Development.FileWatcher, 1.0.1"
#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.
// Install G2Development.FileWatcher as a Cake Addin
#addin nuget:?package=G2Development.FileWatcher&version=1.0.1

// Install G2Development.FileWatcher as a Cake Tool
#tool nuget:?package=G2Development.FileWatcher&version=1.0.1

File Watcher

A File Watching module similar to FileSystemWatcher from Microsoft. The need arose to find a Watching module to monitor file changes inside of a Docker container where the files might be changed by the Host computer. It seems as if these changes is not monitored by FileSystemWatcher.

An attempt was made to keep the naming conventions very close to that of the FileSystemWatcher where possible. The usage will also be similar. Have a look at Diferences section for the differences between the 2 modules.

Idealy it would have been great if FileSystemWatcher of Microsoft would have included both methods for watching file changes. Or allow a property switch to switch between the 2 methods.

Examples

The following example creates a FileWatcher to watch the directory specified at run time. The component is set to watch for changes of any Notification type in the directory. If a file is changed, created, or deleted, the path to the file prints to the console. When a file is renamed, the old and new paths print to the console.

public class Watcher
{
    public static void Main()
    {
        Run();
    }

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    private static void Run()
    {
        string[] args = Environment.GetCommandLineArgs();

        // If a directory is not specified, exit program.
        if (args.Length != 2)
        {
            // Display the proper way to call the program.
            Console.WriteLine("Usage: Watcher.exe (directory)");
            return;
        }

        // Create a new FileWatcher and set its properties.
        using (FileWatcher watcher = new FileWatcher())
        {
            watcher.Path = args[1];

            // Watch for changes in LastAccess and LastWrite times, and
            // the renaming of files or directories.
            watcher.NotifyFilter = NotifyFilters.LastAccess
                                 | NotifyFilters.LastWrite
                                 | NotifyFilters.FileStructure
                                 | NotifyFilters.Size;
                                 | NotifyFilters.Attributes;

            // Only watch text files.
            watcher.Filter = "*.txt";
            // could also use watcher.Filters.Add("*.txt")

            // Add event handlers.
            watcher.Changed += OnChanged;
            watcher.Created += OnChanged;
            watcher.Deleted += OnChanged;

            // Begin watching.
            watcher.EnableRaisingEvents = true;

            // Wait for the user to quit the program.
            Console.WriteLine("Press 'q' to quit the sample.");
            while (Console.Read() != 'q') ;
        }
    }

    // Define the event handlers.
    private static void OnChanged(object source, FileWatcherEventArgs e) =>
        // Specify what is done when a file is changed, created, or deleted.
        Console.WriteLine($"File: {e.FullPath} {e.Type}");
}

Note the above example has been taken from Microsoft's site to illustrate the similarities.

Differences

This module does not implement the following methods:

  • OnError
  • OnRenamed
  • WaitforChange

If follows that this method also do not implement the following events:

  • Error
  • Renamed

The following Properties are not implemented:

  • InternalBufferSize
  • Site
  • SynchronizingObject

Filters are worked differently with this module.

  • This module convert strings to regex friendly strings to find files or folders.
  • It supports /**/*.* structure and ignore separator type (i.e. / or ).
  • All filters can be added either as string values (which will be normalized - make Regex friendly ⇒ . will become .) through either the Filters List or Filter string.
  • In addition to Filter string this component also have an IgnoreFilter String, which include all files that should be removed. This can also be added through the Filters List by including ! at the start of the string.
  • Starting a string with ./ and / will have different effects.
    • ./ ⇒ Will have the effect of adding the path variable to the filter string. I.e. the full path need to match.
    • / ⇒ Will have the effect that any folder with this name will match (i.e. /folder) will match at any level [path/other folders/folder] and [path/folder] will all match.
  • /** - Will include all folders folowing the current folder.
  • /* or some*file - Will include any number of values, but will not include . or / (usually)
  • *.* - will include any number and any type of character except / or .

IncludeSubDirectories might also work differently.

  • Where FileSystemWatcher most likely uses Directory.GetDirectories(Filter) and will get files or folders deeper than the current Path value, Setting IncludeSubDirectories to false will exclude any subdirectories of the Path folder in it's search.

NotifyFilters also has some differences:

  • This module does not include the option for: CreationTime | DirectoryName | Security | FileName.
  • This module includes an additional Filter: FileStructure which is the Filter type to include if Any file-structure event takes place (Adding or deleting files or folders).
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 was computed.  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. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • 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
2.0.1.3 8,738 9/29/2020
2.0.1.2 454 9/29/2020
2.0.1.1 441 9/29/2020
2.0.1 380 9/29/2020
2.0.0 360 9/28/2020
1.0.1 450 9/25/2020
1.0.0 474 9/25/2020

This is the initial Release.