1+ namespace Util . Helpers ;
2+
3+ /// <summary>
4+ /// 文件监视器
5+ /// </summary>
6+ public class FileWatcher : IDisposable {
7+ /// <summary>
8+ /// 文件系统监视器
9+ /// </summary>
10+ private readonly FileSystemWatcher _watcher ;
11+
12+ /// <summary>
13+ /// 初始化文件监视器
14+ /// </summary>
15+ public FileWatcher ( ) {
16+ _watcher = new FileSystemWatcher ( ) ;
17+ _watcher . NotifyFilter = NotifyFilters . LastWrite | NotifyFilters . FileName | NotifyFilters . DirectoryName ;
18+ }
19+
20+ /// <summary>
21+ /// 设置监听目录路径
22+ /// </summary>
23+ /// <param name="path">目录绝对路径</param>
24+ /// <param name="includeSubdirectories">是否监听子目录,默认值: true</param>
25+ public FileWatcher Path ( string path , bool includeSubdirectories = true ) {
26+ _watcher . Path = path ;
27+ _watcher . IncludeSubdirectories = includeSubdirectories ;
28+ return this ;
29+ }
30+
31+ /// <summary>
32+ /// 设置监听通知过滤
33+ /// </summary>
34+ /// <param name="notifyFilters">监听通知过滤</param>
35+ public FileWatcher NotifyFilter ( NotifyFilters notifyFilters ) {
36+ _watcher . NotifyFilter = notifyFilters ;
37+ return this ;
38+ }
39+
40+ /// <summary>
41+ /// 设置过滤模式
42+ /// </summary>
43+ /// <param name="filter">过滤模式,默认值: *.* ,范例: *.cshtml 可过滤cshtml文件</param>
44+ public FileWatcher Filter ( string filter ) {
45+ _watcher . Filter = filter ;
46+ return this ;
47+ }
48+
49+ /// <summary>
50+ /// 处理文件创建监听事件
51+ /// </summary>
52+ /// <param name="action">文件创建监听事件处理器</param>
53+ public FileWatcher OnCreated ( Action < object , FileSystemEventArgs > action ) {
54+ _watcher . Created += ( source , e ) => {
55+ action ( source , e ) ;
56+ } ;
57+ return this ;
58+ }
59+
60+ /// <summary>
61+ /// 处理文件创建监听事件
62+ /// </summary>
63+ /// <param name="action">文件创建监听事件处理器</param>
64+ public FileWatcher OnCreatedAsync ( Func < object , FileSystemEventArgs , Task > action ) {
65+ _watcher . Created += async ( source , e ) => {
66+ await action ( source , e ) ;
67+ } ;
68+ return this ;
69+ }
70+
71+ /// <summary>
72+ /// 处理文件变更监听事件
73+ /// </summary>
74+ /// <param name="action">文件变更监听事件处理器</param>
75+ public FileWatcher OnChanged ( Action < object , FileSystemEventArgs > action ) {
76+ _watcher . Changed += ( source , e ) => {
77+ action ( source , e ) ;
78+ } ;
79+ return this ;
80+ }
81+
82+ /// <summary>
83+ /// 处理文件变更监听事件
84+ /// </summary>
85+ /// <param name="action">文件变更监听事件处理器</param>
86+ public FileWatcher OnChangedAsync ( Func < object , FileSystemEventArgs , Task > action ) {
87+ _watcher . Changed += async ( source , e ) => {
88+ await action ( source , e ) ;
89+ } ;
90+ return this ;
91+ }
92+
93+ /// <summary>
94+ /// 处理文件删除监听事件
95+ /// </summary>
96+ /// <param name="action">文件删除监听事件处理器</param>
97+ public FileWatcher OnDeleted ( Action < object , FileSystemEventArgs > action ) {
98+ _watcher . Deleted += ( source , e ) => {
99+ action ( source , e ) ;
100+ } ;
101+ return this ;
102+ }
103+
104+ /// <summary>
105+ /// 处理文件删除监听事件
106+ /// </summary>
107+ /// <param name="action">文件删除监听事件处理器</param>
108+ public FileWatcher OnDeletedAsync ( Func < object , FileSystemEventArgs , Task > action ) {
109+ _watcher . Deleted += async ( source , e ) => {
110+ await action ( source , e ) ;
111+ } ;
112+ return this ;
113+ }
114+
115+ /// <summary>
116+ /// 处理文件重命名监听事件
117+ /// </summary>
118+ /// <param name="action">文件重命名监听事件处理器</param>
119+ public FileWatcher OnRenamed ( Action < object , RenamedEventArgs > action ) {
120+ _watcher . Renamed += ( source , e ) => {
121+ action ( source , e ) ;
122+ } ;
123+ return this ;
124+ }
125+
126+ /// <summary>
127+ /// 处理文件重命名监听事件
128+ /// </summary>
129+ /// <param name="action">文件重命名监听事件处理器</param>
130+ public FileWatcher OnRenamedAsync ( Func < object , RenamedEventArgs , Task > action ) {
131+ _watcher . Renamed += async ( source , e ) => {
132+ await action ( source , e ) ;
133+ } ;
134+ return this ;
135+ }
136+
137+ /// <summary>
138+ /// 处理文件错误监听事件
139+ /// </summary>
140+ /// <param name="action">文件错误监听事件处理器</param>
141+ public FileWatcher OnError ( Action < object , ErrorEventArgs > action ) {
142+ _watcher . Error += ( source , e ) => {
143+ action ( source , e ) ;
144+ } ;
145+ return this ;
146+ }
147+
148+ /// <summary>
149+ /// 处理文件错误监听事件
150+ /// </summary>
151+ /// <param name="action">文件错误监听事件处理器</param>
152+ public FileWatcher OnErrorAsync ( Func < object , ErrorEventArgs , Task > action ) {
153+ _watcher . Error += async ( source , e ) => {
154+ await action ( source , e ) ;
155+ } ;
156+ return this ;
157+ }
158+
159+ /// <summary>
160+ /// 处理文件监听器释放事件
161+ /// </summary>
162+ /// <param name="action">文件监听器释放事件处理器</param>
163+ public FileWatcher OnDisposed ( Action < object , EventArgs > action ) {
164+ _watcher . Disposed += ( source , e ) => {
165+ action ( source , e ) ;
166+ } ;
167+ return this ;
168+ }
169+
170+ /// <summary>
171+ /// 处理文件监听器释放事件
172+ /// </summary>
173+ /// <param name="action">文件监听器释放事件处理器</param>
174+ public FileWatcher OnDisposedAsync ( Func < object , EventArgs , Task > action ) {
175+ _watcher . Disposed += async ( source , e ) => {
176+ await action ( source , e ) ;
177+ } ;
178+ return this ;
179+ }
180+
181+ /// <summary>
182+ /// 启动监听
183+ /// </summary>
184+ public FileWatcher Start ( ) {
185+ _watcher . EnableRaisingEvents = true ;
186+ return this ;
187+ }
188+
189+ /// <summary>
190+ /// 停止监听
191+ /// </summary>
192+ public FileWatcher Stop ( ) {
193+ _watcher . EnableRaisingEvents = false ;
194+ return this ;
195+ }
196+
197+ /// <summary>
198+ /// 释放
199+ /// </summary>
200+ public void Dispose ( ) {
201+ _watcher ? . Dispose ( ) ;
202+ }
203+ }
0 commit comments