File tree Expand file tree Collapse file tree 4 files changed +106
-5
lines changed Expand file tree Collapse file tree 4 files changed +106
-5
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
1
2
using System . IO ;
2
3
using System . Text . RegularExpressions ;
3
4
@@ -93,5 +94,18 @@ public string GetName(string directory)
93
94
{
94
95
return Path . GetDirectoryName ( directory ) ;
95
96
}
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
+ }
96
110
}
97
- }
111
+ }
Original file line number Diff line number Diff line change 1
- using System . IO ;
1
+ using System ;
2
+ using System . IO ;
2
3
using System . Text ;
3
4
using System . Text . RegularExpressions ;
4
5
using System . Xml . Linq ;
@@ -175,5 +176,21 @@ public string GetName(string path)
175
176
{
176
177
return Path . GetFileName ( path ) ;
177
178
}
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
+ }
178
195
}
179
- }
196
+ }
Original file line number Diff line number Diff line change 1
- using System . IO ;
1
+ using System ;
2
+ using System . IO ;
2
3
using System . Reflection ;
3
4
using System . Text ;
4
5
using System . Xml . Linq ;
@@ -203,9 +204,36 @@ public static string ToDirectory(string directory)
203
204
return directoryHelper . ToDirectory ( directory ) ;
204
205
}
205
206
207
+ public static string GetDirectoryName ( string path )
208
+ {
209
+ return directoryHelper . GetName ( path ) ;
210
+ }
211
+
206
212
public static string GetFileName ( string path )
207
213
{
208
214
return fileHelper . GetName ( path ) ;
209
215
}
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
+ }
210
238
}
211
- }
239
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments