三叔博客 - CSharp
type: channel
link: https://www.sanshu.cn/tag/CSharp/
items: C#监视某个文件夹中的文件变动

C#监视某个文件夹中的文件变动
type: item
date: 2024-09-29T10:46:29+08:00
description: { FileSystemWatcher watcher = new FileSystemWatcher { Path = @"D:\workspace", NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite, Filter = "*.*", IncludeSubdirectories = true }; watcher.Changed += OnChanged; watcher.Created += OnChanged; watcher.Deleted += OnChanged; watcher.Renamed += OnRenamed; watcher.EnableRaisingEvents = true; } // File system change handler private void OnChanged(object source, FileSystemEventArgs e) { UpdateDatabase(e.FullPath, e.ChangeType); } private void OnRenamed(object source, RenamedEventArgs e) { UpdateDatabase(e.OldFullPath, WatcherChangeTypes.Deleted); UpdateDatabase(e.FullPath, WatcherChangeTypes.Created);