From 6a6d04706e0f050173f6e347af2491760c6696e7 Mon Sep 17 00:00:00 2001 From: KY Date: Mon, 9 May 2022 15:29:08 +0200 Subject: [PATCH] - FileSystem watch added - FileSystemWatcherExtension added - FileSystem get last write date added --- .../DataAccess/DirectoryHelper.cs | 16 ++++++- Core.Common.Standard/DataAccess/FileHelper.cs | 21 +++++++++- Core.Common.Standard/DataAccess/FileSystem.cs | 32 +++++++++++++- .../Extension/FileSystemWatcherExtension.cs | 42 +++++++++++++++++++ 4 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 Core.Common.Standard/Extension/FileSystemWatcherExtension.cs diff --git a/Core.Common.Standard/DataAccess/DirectoryHelper.cs b/Core.Common.Standard/DataAccess/DirectoryHelper.cs index 1cd2fbf..3c82a87 100644 --- a/Core.Common.Standard/DataAccess/DirectoryHelper.cs +++ b/Core.Common.Standard/DataAccess/DirectoryHelper.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Text.RegularExpressions; @@ -93,5 +94,18 @@ public string GetName(string directory) { return Path.GetDirectoryName(directory); } + + public FileSystemWatcher Watch(string path) + { + string absolutePath = this.pathHelper.ToAbsolute(path); + FileSystemWatcher watchdog = new (absolutePath); + watchdog.EnableRaisingEvents = true; + return watchdog; + } + + public DateTime GetLastWriteTime(params string[] pathChunks) + { + return Directory.GetLastWriteTime(this.pathHelper.ToAbsolute(pathChunks)); + } } -} \ No newline at end of file +} diff --git a/Core.Common.Standard/DataAccess/FileHelper.cs b/Core.Common.Standard/DataAccess/FileHelper.cs index d6f63eb..d3dd45f 100644 --- a/Core.Common.Standard/DataAccess/FileHelper.cs +++ b/Core.Common.Standard/DataAccess/FileHelper.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Xml.Linq; @@ -175,5 +176,21 @@ public string GetName(string path) { return Path.GetFileName(path); } + + public FileSystemWatcher Watch(string path) + { + string absolutePath = FileSystem.ToAbsolutePath(path); + string directory = FileSystem.GetDirectoryName(absolutePath); + string file = FileSystem.GetFileName(absolutePath); + FileSystemWatcher watchdog = new (directory); + watchdog.Filter = file; + watchdog.EnableRaisingEvents = true; + return watchdog; + } + + public DateTime GetLastWriteTime(params string[] pathChunks) + { + return File.GetLastWriteTime(this.pathHelper.ToAbsolute(pathChunks)); + } } -} \ No newline at end of file +} diff --git a/Core.Common.Standard/DataAccess/FileSystem.cs b/Core.Common.Standard/DataAccess/FileSystem.cs index b379d7b..302f35d 100644 --- a/Core.Common.Standard/DataAccess/FileSystem.cs +++ b/Core.Common.Standard/DataAccess/FileSystem.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Reflection; using System.Text; using System.Xml.Linq; @@ -203,9 +204,36 @@ public static string ToDirectory(string directory) return directoryHelper.ToDirectory(directory); } + public static string GetDirectoryName(string path) + { + return directoryHelper.GetName(path); + } + public static string GetFileName(string path) { return fileHelper.GetName(path); } + + public static FileSystemWatcher Watch(string path) + { + if (DirectoryExists(path)) + { + return directoryHelper.Watch(path); + } + if (FileExists(path)) + { + return fileHelper.Watch(path); + } + throw new InvalidOperationException($"Can not watch file or directory '{path}': Not found."); + } + + public static DateTime GetLastWriteTime(params string[] pathChunks) + { + if (DirectoryExists(pathChunks)) + { + return directoryHelper.GetLastWriteTime(pathChunks); + } + return fileHelper.GetLastWriteTime(pathChunks); + } } -} \ No newline at end of file +} diff --git a/Core.Common.Standard/Extension/FileSystemWatcherExtension.cs b/Core.Common.Standard/Extension/FileSystemWatcherExtension.cs new file mode 100644 index 0000000..34496de --- /dev/null +++ b/Core.Common.Standard/Extension/FileSystemWatcherExtension.cs @@ -0,0 +1,42 @@ +using System.IO; + +namespace KY.Core.Extension; + +public static class FileSystemWatcherExtension +{ + public static FileSystemWatcher OnChanged(this FileSystemWatcher watcher, FileSystemEventHandler handler) + { + watcher.Changed += handler; + return watcher; + } + + public static FileSystemWatcher OnCreated(this FileSystemWatcher watcher, FileSystemEventHandler handler) + { + watcher.Created += handler; + return watcher; + } + + public static FileSystemWatcher OnDeleted(this FileSystemWatcher watcher, FileSystemEventHandler handler) + { + watcher.Deleted += handler; + return watcher; + } + + public static FileSystemWatcher OnRenamed(this FileSystemWatcher watcher, RenamedEventHandler handler) + { + watcher.Renamed += handler; + return watcher; + } + + public static FileSystemWatcher OnError(this FileSystemWatcher watcher, ErrorEventHandler handler) + { + watcher.Error += handler; + return watcher; + } + + public static FileSystemWatcher IncludeSubdirectories (this FileSystemWatcher watcher) + { + watcher.IncludeSubdirectories = true; + return watcher; + } +}