FileSystemWatcher 无法检测到 1 个文件的更改(添加新文件),只能检测到至少 2 个文件

FileSystemWatcher can not detect the change of 1 file (add a new file), only detect atleast 2 files

FileSystemWatcher 无法检测到 1 个文件的更改(添加新文件),只能检测到至少 2 个文件。 watcher 仅在以下情况下调用 Changed 事件:

  • Copy more than 1 files to C:\PortViewer
  • Copy only 1 file to C:\PortViewer, and I have to open C:\PortViewer in the File Explorer of 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;
        }

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;
        }