Class FileWatcher
Provides a watcher implementation designed to monitor and signal changes applied to a file by raising the Changed event.
public class FileWatcher : Watcher, IWatcher, IDisposable
- Inheritance
-
FileWatcher
- Implements
- Inherited Members
Examples
The following example shows how to start a file watcher, observe change notifications, and pause signaling again.
using System;
using System.IO;
using System.Threading;
using Cuemon.Runtime;
namespace MyApp.Examples;
public static class FileWatcherExample
{
public static void Demonstrate()
{
string filePath = Path.Combine(Environment.CurrentDirectory, "health.txt");
File.WriteAllText(filePath, "ready");
using var watcher = new FileWatcher(filePath, readFile: true, options =>
{
options.DueTime = TimeSpan.Zero;
options.Period = TimeSpan.FromSeconds(5);
options.DueTimeOnChanged = TimeSpan.FromMilliseconds(250);
});
watcher.Changed += static (_, e) => Console.WriteLine($"{e.UtcLastModified:O} ({e.Delayed.TotalMilliseconds} ms)");
watcher.StartMonitoring();
Console.WriteLine(watcher.Path);
Console.WriteLine(watcher.ReadFile);
Console.WriteLine(watcher.UtcCreated.ToString("O"));
watcher.ChangeSignaling(Timeout.InfiniteTimeSpan);
}
}
Constructors
FileWatcher(string, bool, Action<WatcherOptions>)
Initializes a new instance of the FileWatcher class.
public FileWatcher(string path, bool readFile = false, Action<WatcherOptions> setup = null)
Parameters
pathstringThe file to monitor, in standard or Universal Naming Convention (UNC) notation.
readFileboolif set to
truethe file specified inpathwill be opened and a checksum will be computed using CyclicRedundancyCheck64 algorithm.setupAction<WatcherOptions>The WatcherOptions which may be configured.
Properties
Checksum
Gets the checksum that is associated with the file specified in Path.
public string Checksum { get; }
Property Value
Remarks
If ReadFile is false this property will remain null.
Path
Gets the path of the file to watch.
public string Path { get; }
Property Value
- string
The path to monitor.
ReadFile
Gets a value indicating whether the file specified in Path will be opened, read and assign the computed value to Checksum.
public bool ReadFile { get; }
Property Value
- bool
trueif the file specified in Path will be opened, read and assign the computed value to Checksum; otherwise,false.
UtcCreated
Gets the time, in Coordinated Universal Time (UTC), when this instance was created.
public DateTime UtcCreated { get; }
Property Value
- DateTime
The time, in Coordinated Universal Time (UTC), when this instance was created.
Methods
HandleSignalingAsync()
Handles the signaling of this FileWatcher.
protected override Task HandleSignalingAsync()