FileSystemWatcher无法检测到 1 文件的更改(添加新文件),只能检测到至少 2 个文件。 观察者仅在以下情况下调用 Changed 事件:
- 将超过 1 个文件复制到 C:\ PortViewer
- 仅将 1 文件复制到 C:\ PortViewer ,我必须在文件资源管理器中打开 C:\ PortViewer ( Windows )。
FileSystemWatcher watcher;
private void StartWatcher()
{
Directory.CreateDirectory(@"C:\PortViewer");
watcher = new FileSystemWatcher();
watcher.Path = @"C:\PortViewer";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.*";
watcher.Changed += (s,e) => { /*Do work here*/ };
watcher.EnableRaisingEvents = true;
}
答案 0 :(得分:0)
NotifyFilters.CreatedTime
是解决方案:
FileSystemWatcher watcher;
private void StartWatcher()
{
Directory.CreateDirectory(@"C:\PortViewer");
watcher = new FileSystemWatcher();
watcher.Path = @"C:\PortViewer";
watcher.NotifyFilter = NotifyFilters.CreatedTime;
watcher.Filter = "*.*";
watcher.Changed += (s,e) => { /*Do work here*/ };
watcher.EnableRaisingEvents = true;
}