Table of Contents

FileWatcher Class

Definition

Namespace
Cuemon.Runtime
Assemblies
Cuemon.Core.dll
Source
src/Cuemon.Core/Runtime/FileWatcher.cs

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

Name Description
FileWatcher(string, bool, Action<WatcherOptions>)

Initializes a new instance of the FileWatcher class.

Properties

Name Description
Checksum

Gets the checksum that is associated with the file specified in Path.

Path

Gets the path of the file to watch.

ReadFile

Gets a value indicating whether the file specified in Path will be opened, read and assign the computed value to Checksum.

UtcCreated

Gets the time, in Coordinated Universal Time (UTC), when this instance was created.

Methods

Name Description
HandleSignalingAsync()

Handles the signaling of this FileWatcher.

See Also