44
55namespace FileWatcherEx ;
66
7+ /// <summary>
8+ /// A wrapper of <see cref="FileSystemWatcher"/> to standardize the events
9+ /// and avoid false change notifications.
10+ /// </summary>
711public class FileSystemWatcherEx : IDisposable
812{
913
@@ -26,26 +30,23 @@ public class FileSystemWatcherEx : IDisposable
2630 #region Public Properties
2731
2832 /// <summary>
29- /// Folder path to watch
33+ /// Gets or sets the path of the directory to watch.
3034 /// </summary>
3135 public string FolderPath { get ; set ; } = "" ;
3236
3337
3438 /// <summary>
35- /// The collection of all the filters used to determine what files are monitored in a directory.
39+ /// Gets the collection of all the filters used to determine what files are monitored in a directory.
3640 /// </summary>
3741 public System . Collections . ObjectModel . Collection < string > Filters { get ; } = new ( ) ;
3842
3943
4044 /// <summary>
41- /// Filter string used for determining what files are monitored in a directory
45+ /// Gets or sets the filter string used to determine what files are monitored in a directory.
4246 /// </summary>
4347 public string Filter
4448 {
45- get
46- {
47- return Filters . Count == 0 ? "*" : Filters [ 0 ] ;
48- }
49+ get => Filters . Count == 0 ? "*" : Filters [ 0 ] ;
4950 set
5051 {
5152 Filters . Clear ( ) ;
@@ -55,7 +56,11 @@ public string Filter
5556
5657
5758 /// <summary>
58- /// Gets, sets the type of changes to watch for
59+ /// Gets or sets the type of changes to watch for.
60+ /// The default is the bitwise OR combination of
61+ /// <see cref="NotifyFilters.LastWrite"/>,
62+ /// <see cref="NotifyFilters.FileName"/>,
63+ /// and <see cref="NotifyFilters.DirectoryName"/>.
5964 /// </summary>
6065 public NotifyFilters NotifyFilter { get ; set ; } = NotifyFilters . LastWrite | NotifyFilters . FileName | NotifyFilters . DirectoryName ;
6166
@@ -76,31 +81,57 @@ public string Filter
7681
7782
7883 #region Public Events
79- public delegate void DelegateOnChanged ( object ? sender , FileChangedEvent e ) ;
84+
85+ /// <summary>
86+ /// Occurs when a file or directory in the specified
87+ /// <see cref="FolderPath"/> is changed.
88+ /// </summary>
8089 public event DelegateOnChanged ? OnChanged ;
90+ public delegate void DelegateOnChanged ( object ? sender , FileChangedEvent e ) ;
8191
82- public delegate void DelegateOnDeleted ( object ? sender , FileChangedEvent e ) ;
92+
93+ /// <summary>
94+ /// Occurs when a file or directory in the specified
95+ /// <see cref="FolderPath"/> is deleted.
96+ /// </summary>
8397 public event DelegateOnDeleted ? OnDeleted ;
98+ public delegate void DelegateOnDeleted ( object ? sender , FileChangedEvent e ) ;
8499
85- public delegate void DelegateOnCreated ( object ? sender , FileChangedEvent e ) ;
100+
101+ /// <summary>
102+ /// Occurs when a file or directory in the specified
103+ /// <see cref="FolderPath"/> is created.
104+ /// </summary>
86105 public event DelegateOnCreated ? OnCreated ;
106+ public delegate void DelegateOnCreated ( object ? sender , FileChangedEvent e ) ;
87107
88- public delegate void DelegateOnRenamed ( object ? sender , FileChangedEvent e ) ;
108+
109+ /// <summary>
110+ /// Occurs when a file or directory in the specified
111+ /// <see cref="FolderPath"/> is renamed.
112+ /// </summary>
89113 public event DelegateOnRenamed ? OnRenamed ;
114+ public delegate void DelegateOnRenamed ( object ? sender , FileChangedEvent e ) ;
90115
91- public delegate void DelegateOnError ( object ? sender , ErrorEventArgs e ) ;
116+
117+ /// <summary>
118+ /// Occurs when the instance of <see cref="FileSystemWatcherEx"/> is unable to continue
119+ /// monitoring changes or when the internal buffer overflows.
120+ /// </summary>
92121 public event DelegateOnError ? OnError ;
122+ public delegate void DelegateOnError ( object ? sender , ErrorEventArgs e ) ;
123+
93124 #endregion
94125
95126
96127
97128 /// <summary>
98- /// Initialize new instance of FileWatcherEx
129+ /// Initialize new instance of <see cref="FileSystemWatcherEx"/>
99130 /// </summary>
100- /// <param name="folder "></param>
101- public FileSystemWatcherEx ( string folder = "" )
131+ /// <param name="folderPath "></param>
132+ public FileSystemWatcherEx ( string folderPath = "" )
102133 {
103- FolderPath = folder ;
134+ FolderPath = folderPath ;
104135 }
105136
106137
@@ -244,25 +275,6 @@ void onError(ErrorEventArgs e)
244275 _fsw . EnableRaisingEvents = true ;
245276 }
246277
247- private void Thread_DoingWork ( CancellationToken cancelToken )
248- {
249- while ( true )
250- {
251- if ( cancelToken . IsCancellationRequested )
252- return ;
253-
254- try
255- {
256- var e = _fileEventQueue . Take ( cancelToken ) ;
257- _processor ? . ProcessEvent ( e ) ;
258- }
259- catch ( OperationCanceledException )
260- {
261- return ;
262- }
263- }
264- }
265-
266278
267279 /// <summary>
268280 /// Stop watching files
@@ -284,7 +296,6 @@ public void Stop()
284296 }
285297
286298
287-
288299 /// <summary>
289300 /// Dispose the FileWatcherEx instance
290301 /// </summary>
@@ -297,4 +308,26 @@ public void Dispose()
297308 }
298309 }
299310
311+
312+
313+ private void Thread_DoingWork ( CancellationToken cancelToken )
314+ {
315+ while ( true )
316+ {
317+ if ( cancelToken . IsCancellationRequested )
318+ return ;
319+
320+ try
321+ {
322+ var e = _fileEventQueue . Take ( cancelToken ) ;
323+ _processor ? . ProcessEvent ( e ) ;
324+ }
325+ catch ( OperationCanceledException )
326+ {
327+ return ;
328+ }
329+ }
330+ }
331+
332+
300333}
0 commit comments