using XFEExtension.NetCore.TaskExtension; namespace XFEExtension.NetCore.FileExtension; /// /// XFE文件监视器 /// public class XFEFileWatcher { #region 字段 private readonly List watchers = []; #endregion #region 属性 /// /// 是否遍历监视所有子文件夹 /// public bool WatchSubdirectories { get; set; } /// /// 文件或文件夹路径 /// public string? Path { get; set; } #endregion #region 事件 /// /// 文件发生改变时触发 /// public event FileSystemEventHandler? FileChanged; /// /// 文件被创建时触发 /// public event FileSystemEventHandler? FileCreated; /// /// 文件被删除时触发 /// public event FileSystemEventHandler? FileDeleted; /// /// 文件被重命名时触发 /// public event RenamedEventHandler? FileRenamed; #endregion #region 方法 private void OnFileChanged(object sender, FileSystemEventArgs e) { switch (e.ChangeType) { case WatcherChangeTypes.Changed: FileChanged?.Invoke(sender, e); break; case WatcherChangeTypes.Created: FileCreated?.Invoke(sender, e); break; case WatcherChangeTypes.Deleted: FileDeleted?.Invoke(sender, e); break; default: break; } } private void OnFileRenamed(object sender, RenamedEventArgs e) { FileRenamed?.Invoke(sender, e); } private void MonitorSubdirectories(string folderPath) { string[] subdirectories = Directory.GetDirectories(folderPath); foreach (string subdirectory in subdirectories) { try { FileSystemWatcher subdirectoryWatcher = new(subdirectory) { NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName }; watchers.Add(subdirectoryWatcher); subdirectoryWatcher.Changed += OnFileChanged; subdirectoryWatcher.Created += OnFileChanged; subdirectoryWatcher.Deleted += OnFileChanged; subdirectoryWatcher.Renamed += OnFileRenamed; subdirectoryWatcher.EnableRaisingEvents = true; } catch (Exception ex) { Console.WriteLine(ex.Message); continue; } MonitorSubdirectories(subdirectory); } } /// /// 启动监视 /// /// /// public async Task StartWatchingAsync() { if (Path is not null && Path != string.Empty) { if (File.Exists(Path) || Directory.Exists(Path)) { if (WatchSubdirectories && Directory.Exists(Path)) { await new Action(() => { MonitorSubdirectories(Path); }).StartNewTask(); } FileSystemWatcher rootWatcher = new(Path); watchers.Add(rootWatcher); rootWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; rootWatcher.Changed += OnFileChanged; rootWatcher.Created += OnFileChanged; rootWatcher.Deleted += OnFileChanged; rootWatcher.Renamed += OnFileRenamed; } else { throw new XFEExtensionException("文件或文件夹不存在"); } } else { throw new XFEExtensionException("未设置属性"); } } /// /// 启动监视 /// /// /// public void StartWatching() { if (Path is not null && Path != string.Empty) { FileSystemWatcher rootWatcher; if (Directory.Exists(Path)) { if (WatchSubdirectories) { MonitorSubdirectories(Path); } rootWatcher = new(Path); } else if (File.Exists(Path)) { rootWatcher = new(System.IO.Path.GetDirectoryName(Path)!) { Filter = System.IO.Path.GetFileName(Path) }; } else { throw new XFEExtensionException("文件或文件夹不存在"); } watchers.Add(rootWatcher); rootWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; rootWatcher.Changed += OnFileChanged; rootWatcher.Created += OnFileChanged; rootWatcher.Deleted += OnFileChanged; rootWatcher.Renamed += OnFileRenamed; rootWatcher.EnableRaisingEvents = true; } else { throw new XFEExtensionException("未设置属性"); } } /// /// 停止监视 /// public void StopWatching() { foreach (FileSystemWatcher watcher in watchers) { watcher.EnableRaisingEvents = false; } } /// /// 继续监视 /// public void ContinueWatching() { foreach (FileSystemWatcher watcher in watchers) { watcher.EnableRaisingEvents = true; } } /// /// 释放资源 /// public void Dispose() { foreach (FileSystemWatcher watcher in watchers) { watcher.Dispose(); } } #endregion #region 构造函数 /// /// XFE文件监视器 /// public XFEFileWatcher() { } /// /// XFE文件监视器 /// /// 路径 /// 是否遍历监视所有子文件/文件夹 public XFEFileWatcher(string path, bool watchSubdirectories = false) { Path = path; WatchSubdirectories = watchSubdirectories; } #endregion }