An event detected by the watcher.
Beispiel #1
0
 /// <summary>
 /// Get the change queue.
 /// </summary>
 public Queue <WatcherEvent> GetChangeQueue()
 {
     lock (changeLock)
     {
         Queue <WatcherEvent> changeQueue = new Queue <WatcherEvent>();
         int changeListCount = changeList.Count;
         for (int i = 0; i < changeListCount; i++)
         {
             WatcherEvent        watcherEvent = changeList[i];
             FileSystemEventArgs change       = watcherEvent.GetFileSystemEventArgs();
             string fileName        = System.IO.Path.GetFileName(change.FullPath);
             string dirName         = System.IO.Path.GetDirectoryName(change.FullPath);
             bool   redundantChange = false;
             if (change.ChangeType == WatcherChangeTypes.Deleted)
             {
                 //Detect a file/folder move...
                 FileSystemEventArgs nextChange = ((i + 1) < changeListCount) ? changeList[i + 1].GetFileSystemEventArgs() : null;
                 if (nextChange != null)
                 {
                     string nextFileName = System.IO.Path.GetFileName(nextChange.FullPath);
                     string nextDirName  = System.IO.Path.GetDirectoryName(nextChange.FullPath);
                     if (nextChange.ChangeType == WatcherChangeTypes.Created &&
                         nextFileName.Equals(fileName) && !nextDirName.Equals(dirName))
                     {
                         //Move detected...
                         change = new MovedEventArgs(WatcherChangeTypes.Renamed, dirName, nextDirName, fileName);
                         ++i; //Skip nextChange
                     }
                 }
             }
             else if (change.ChangeType == WatcherChangeTypes.Changed)
             {
                 //Detect redundant changes...
                 for (int j = i - 1; j >= 0; j--)
                 {
                     //Iterate backwards through the list of changes, find the most recent operation on this specific file
                     if (change.FullPath.Equals(changeList[j].GetFileSystemEventArgs().FullPath))
                     {
                         //If the most recent operation is a created or changed operation this operation is redundant
                         if (changeList[j].GetFileSystemEventArgs().ChangeType == WatcherChangeTypes.Created ||
                             changeList[j].GetFileSystemEventArgs().ChangeType == WatcherChangeTypes.Changed)
                         {
                             redundantChange = true;
                         }
                         break;
                     }
                 }
             }
             if (redundantChange)
             {
                 Logger.DebugFormat("Local file change {0} discarded because redundant", change.ChangeType);
             }
             else
             {
                 changeQueue.Enqueue(watcherEvent);
             }
         }
         return(changeQueue);
     }
 }