FileSystemWatcher does not report changes in a locked file

后端 未结 6 884
清酒与你
清酒与你 2021-01-01 18:26

I\'m monitoring a folder using a FileSystemWatcher like this:

watcher = new FileSystemWatcher(folder);
watcher.NotifyFilter = NotifyFilters.Size;
watcher.Cha         


        
6条回答
  •  心在旅途
    2021-01-01 18:55

    You indeed need a thread that would open all files and read a byte from them, my program stopped working after I ran it on Windows 7 instead of XP, I used the following code

    private void SingleByteReadThread(object notUsed)
        {
           while (true)
          {
             foreach (FileInfo fi in new DirectoryEnumerator(folderPath))
                      {
                          using (FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                              fs.ReadByte();    
                      }
    
              Thread.Sleep(TimeSpan.FromSeconds(2));
          }
      }
    

    DirectoryEnumerator is my own class

提交回复
热议问题