Skip to content

Commit

Permalink
- FileSystem watch added
Browse files Browse the repository at this point in the history
- FileSystemWatcherExtension added
- FileSystem get last write date added
  • Loading branch information
ky-one committed May 9, 2022
1 parent 6b7e0b6 commit 6a6d047
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 5 deletions.
16 changes: 15 additions & 1 deletion Core.Common.Standard/DataAccess/DirectoryHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -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));
}
}
}
}
21 changes: 19 additions & 2 deletions Core.Common.Standard/DataAccess/FileHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
Expand Down Expand Up @@ -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));
}
}
}
}
32 changes: 30 additions & 2 deletions Core.Common.Standard/DataAccess/FileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
Expand Down Expand Up @@ -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);
}
}
}
}
42 changes: 42 additions & 0 deletions Core.Common.Standard/Extension/FileSystemWatcherExtension.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 6a6d047

Please sign in to comment.