Running the recovery filewatcher

Our recovery filewatcher is run via a method of the same name. Within the function, we will respond to the various events in place. For each event we will display the message to the console output and then create and post a filesystem message with the results:

void RunRecoveringWatcher()
{
Console.WriteLine("Will auto-detect unavailability of watched directory");
Console.WriteLine(" - Windows timeout accessing network shares: ~110 sec on start, ~45 sec while watching.");
using (var watcher = new RecoveringFileSystemWatcher(TestPath))
{
watcher.All += (_, e) => { WriteLineInColor($"{e.ChangeType} {e.Name}", ConsoleColor.Yellow); };
watcher.Error += (_, e) =>
{
WriteLineInColor(e.Error.Message, ConsoleColor.Red);
};
watcher.Existed += (_, e) =>
{
WriteLineInColor($"Existed {e.Name}", ConsoleColor.Yellow);
};
watcher.Created += (_, e) =>
{
WriteLineInColor($"Created {e.Name}", ConsoleColor.Yellow);
FileSystemChangeMessage m = new FileSystemChangeMessage
{
ChangeDate = SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc().ToLocalTime(),
ChangeType = (int)e.ChangeType,
FullPath = e.FullPath,
Name = e.Name
};
_bus.Publish(m, "FileSystemChanges");
};
watcher.Deleted += (_, e) =>
{
WriteLineInColor($"Deleted {e.Name}", ConsoleColor.Yellow);
FileSystemChangeMessage m = new FileSystemChangeMessage
{
ChangeDate = SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc().ToLocalTime(),
ChangeType = (int)e.ChangeType,
FullPath = e.FullPath,
Name = e.Name
};
_bus.Publish(m, "FileSystemChanges");
};
watcher.Renamed += (_, e) =>
{
WriteLineInColor($"Renamed {e.OldName} to {e.Name}", ConsoleColor.Yellow);
FileSystemChangeMessage m = new FileSystemChangeMessage
{
ChangeDate = SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc().ToLocalTime(),
ChangeType = (int)e.ChangeType,
FullPath = e.FullPath,
OldPath = e.OldFullPath,
Name = e.Name,
OldName = e.OldName
};
_bus.Publish(m, "FileSystemChanges");
};
watcher.Changed += (_, e) =>
{
WriteLineInColor($"Changed {e.Name}", ConsoleColor.Yellow);
FileSystemChangeMessage m = new FileSystemChangeMessage
{
ChangeDate = SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc().ToLocalTime(),
ChangeType = (int)e.ChangeType,
FullPath = e.FullPath,
Name = e.Name
};
_bus.Publish(m, "FileSystemChanges");
};
watcher.DirectoryMonitorInterval = TimeSpan.FromSeconds(10);
watcher.OrderByOldestFirst = false;
watcher.DirectoryRetryInterval = TimeSpan.FromSeconds(5);
watcher.IncludeSubdirectories = false;
//watcher.EventQueueCapacity = 1;
watcher.EnableRaisingEvents = true;
PromptUser("Processing...");
Console.WriteLine("Stopping...");
}
PromptUser("Stopped.");
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset