Skip to content

Commit 6a6d047

Browse files
committed
- FileSystem watch added
- FileSystemWatcherExtension added - FileSystem get last write date added
1 parent 6b7e0b6 commit 6a6d047

File tree

4 files changed

+106
-5
lines changed

4 files changed

+106
-5
lines changed

Core.Common.Standard/DataAccess/DirectoryHelper.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.IO;
23
using System.Text.RegularExpressions;
34

@@ -93,5 +94,18 @@ public string GetName(string directory)
9394
{
9495
return Path.GetDirectoryName(directory);
9596
}
97+
98+
public FileSystemWatcher Watch(string path)
99+
{
100+
string absolutePath = this.pathHelper.ToAbsolute(path);
101+
FileSystemWatcher watchdog = new (absolutePath);
102+
watchdog.EnableRaisingEvents = true;
103+
return watchdog;
104+
}
105+
106+
public DateTime GetLastWriteTime(params string[] pathChunks)
107+
{
108+
return Directory.GetLastWriteTime(this.pathHelper.ToAbsolute(pathChunks));
109+
}
96110
}
97-
}
111+
}

Core.Common.Standard/DataAccess/FileHelper.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Text;
34
using System.Text.RegularExpressions;
45
using System.Xml.Linq;
@@ -175,5 +176,21 @@ public string GetName(string path)
175176
{
176177
return Path.GetFileName(path);
177178
}
179+
180+
public FileSystemWatcher Watch(string path)
181+
{
182+
string absolutePath = FileSystem.ToAbsolutePath(path);
183+
string directory = FileSystem.GetDirectoryName(absolutePath);
184+
string file = FileSystem.GetFileName(absolutePath);
185+
FileSystemWatcher watchdog = new (directory);
186+
watchdog.Filter = file;
187+
watchdog.EnableRaisingEvents = true;
188+
return watchdog;
189+
}
190+
191+
public DateTime GetLastWriteTime(params string[] pathChunks)
192+
{
193+
return File.GetLastWriteTime(this.pathHelper.ToAbsolute(pathChunks));
194+
}
178195
}
179-
}
196+
}

Core.Common.Standard/DataAccess/FileSystem.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Reflection;
34
using System.Text;
45
using System.Xml.Linq;
@@ -203,9 +204,36 @@ public static string ToDirectory(string directory)
203204
return directoryHelper.ToDirectory(directory);
204205
}
205206

207+
public static string GetDirectoryName(string path)
208+
{
209+
return directoryHelper.GetName(path);
210+
}
211+
206212
public static string GetFileName(string path)
207213
{
208214
return fileHelper.GetName(path);
209215
}
216+
217+
public static FileSystemWatcher Watch(string path)
218+
{
219+
if (DirectoryExists(path))
220+
{
221+
return directoryHelper.Watch(path);
222+
}
223+
if (FileExists(path))
224+
{
225+
return fileHelper.Watch(path);
226+
}
227+
throw new InvalidOperationException($"Can not watch file or directory '{path}': Not found.");
228+
}
229+
230+
public static DateTime GetLastWriteTime(params string[] pathChunks)
231+
{
232+
if (DirectoryExists(pathChunks))
233+
{
234+
return directoryHelper.GetLastWriteTime(pathChunks);
235+
}
236+
return fileHelper.GetLastWriteTime(pathChunks);
237+
}
210238
}
211-
}
239+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.IO;
2+
3+
namespace KY.Core.Extension;
4+
5+
public static class FileSystemWatcherExtension
6+
{
7+
public static FileSystemWatcher OnChanged(this FileSystemWatcher watcher, FileSystemEventHandler handler)
8+
{
9+
watcher.Changed += handler;
10+
return watcher;
11+
}
12+
13+
public static FileSystemWatcher OnCreated(this FileSystemWatcher watcher, FileSystemEventHandler handler)
14+
{
15+
watcher.Created += handler;
16+
return watcher;
17+
}
18+
19+
public static FileSystemWatcher OnDeleted(this FileSystemWatcher watcher, FileSystemEventHandler handler)
20+
{
21+
watcher.Deleted += handler;
22+
return watcher;
23+
}
24+
25+
public static FileSystemWatcher OnRenamed(this FileSystemWatcher watcher, RenamedEventHandler handler)
26+
{
27+
watcher.Renamed += handler;
28+
return watcher;
29+
}
30+
31+
public static FileSystemWatcher OnError(this FileSystemWatcher watcher, ErrorEventHandler handler)
32+
{
33+
watcher.Error += handler;
34+
return watcher;
35+
}
36+
37+
public static FileSystemWatcher IncludeSubdirectories (this FileSystemWatcher watcher)
38+
{
39+
watcher.IncludeSubdirectories = true;
40+
return watcher;
41+
}
42+
}

0 commit comments

Comments
 (0)