G2Development.FileWatcher
2.0.0
See the version list below for details.
dotnet add package G2Development.FileWatcher --version 2.0.0
NuGet\Install-Package G2Development.FileWatcher -Version 2.0.0
<PackageReference Include="G2Development.FileWatcher" Version="2.0.0" />
paket add G2Development.FileWatcher --version 2.0.0
#r "nuget: G2Development.FileWatcher, 2.0.0"
// Install G2Development.FileWatcher as a Cake Addin #addin nuget:?package=G2Development.FileWatcher&version=2.0.0 // Install G2Development.FileWatcher as a Cake Tool #tool nuget:?package=G2Development.FileWatcher&version=2.0.0
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 and Official Page
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. This is similare to FileSystemWatcher.
public class Watcher
{
public static void Main()
{
Run();
}
private static void Run()
{
string path = "../../../test";
// Create a new FileWatcher and set its properties.
using FileWatcher watcher = new FileWatcher
{
Path = path,
// Watch for changes in LastAccess and LastWrite times, and
// the renaming of files or directories.
NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName,
IncludeSubdirectories = true,
// Only watch text files.
Filter = "*.txt"
};
// Add event handlers.
watcher.Changed += OnChanged;
watcher.Created += OnChanged;
watcher.Deleted += OnChanged;
watcher.Renamed += OnRenamed;
// 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, FileSystemEventArgs e) =>
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine($"File: {e.FullPath} {e.ChangeType}");
private static void OnRenamed(object source, RenamedEventArgs e) =>
// Specify what is done when a file is renamed.
Console.WriteLine($"File: {e.OldFullPath} renamed to {e.FullPath}");
}
Note the above example has been taken from Microsoft's site to illustrate similarities.
FileWatcher Details
Similarly to FileSystemWatcher propertes, but have in addition:
- Duration (int) - Get or Set the duration of the time taken between File Monitoring (i.e. how regularly the files are monitored in ms). Default = 250
- IgnoreFilter (string)- Works together with the Filter Property, but files filtered by these values will be ignored and not be monitored. Please note that this value will be automatically filled out if Filters properties have negative values (i.e. prefixed with !) and this property is set to an Empty String value.
- IgnoreNormalize (bool) - By Default values set in Filter, Filters and IgnoreFilter properties will be normalized (i.e. made Regex Friendly). If string values should not be made Regex Friendly this property should be set to True. Default=False
- Quick (bool) - By default the Watcher will run every ms as set by the Duration property. It follows that it might happen that multiple events should have been sent during the gap between runs. If Quick is set to True, then only the first event of a Specific Type will be called, and not, else and event for every single difference between the 2 states will be called. Default=False
- AccessSense (AccessSensitivity) - Mostly relate to the case where DirectoryName and LastAccess NotifyFilter is set. And set how sensitive the events should be. i.e. accessing a subfolder will also mean you accessed it's parent. Hence an event could be sent on each access level. Refer to AccessSensitivity for more details.
AccessSensitivity (Enum)
The following options are available:
- Restraint - When Creating, Deleting or Renaming Directories, will initiate event only at the closest directory to where the event took place.
- Normal - When Creating, Deleting or Renaming Directories, Will initiate event for every affected Directory, starting at the root (top most watched parent) directory.
- Aggressive - Will call events as per Normal, but will also include all mouse movements that take place over an open file that is in that folder.
Wrapper
The Wrapper should function exactly the same as the FileWatcher. It basically wraps these 2 objects to make switching between the 2 more dynamic in nature.
For more information refer to FileWatcher page.
Differences
This module does not implement the following methods:
- OnError
- WaitforChange
It follows that this method also do not implement the following events:
- Error
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(Directory, 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.
Events The 2 items might have different results, but overall the FileWatcher class should capture all events called by FileSystemWatcher
Product | Versions 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. |
-
.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.
FileWatcher brought more in line with FileSystemWatcher and added an additional FileWatcherWrapper class.