DetectChanges 始终检测更改

DetectChanges always detect changes

我使用 Microsoft SyncFramework 2.1。

我将远程源目录与本地目标目录同步。 因此,如果框架检测到更改 - 它会将更改从源(远程)下载到目标(本地)。

但是:

DetectChanges 始终检测更改,但目录未更改。

远程 目录包含 1 个文件。

所以,我写代码来同步它:

 public class SyncService
{
  private FileSyncProvider _provider;    
  private FileSyncOptions _syncOptions;
  private FileSyncScopeFilter _filter;
  private string _toLocalDirPath;
  private string _fromSourceDirectory;
  private string _lastFromSourceDirectory; // save last directory (it can be changed);

  public SyncService(string localDirPath,string 
                  fromSourceDirectory)
 {
  _syncOptions = FileSyncOptions.ExplicitDetectChanges |
                FileSyncOptions.RecycleDeletedFiles | 
                FileSyncOptions.RecyclePreviousFileOnUpdates |
                 FileSyncOptions.RecycleConflictLoserFiles;
  _filter = new FileSyncScopeFilter();
 _toLocalDirPath=localDirPath;
 _fromSourceDirectory=fromSourceDirectory;
  }


 public void Sync()
 {
  if (_lastFromSourceDirectory !=Constants.FromSourceDirectory)  //if directory path changed - we should dispose old provider and create new
    {
  if (_provider != null)
     {
    _provider.DetectedChanges -= Provider_DetectedChanges;
    _provider.ApplyingChange -= Provider_ApplyingChange;
    _provider.AppliedChange -= Provider_AppliedChange;
    _provider.CopyingFile -= Provider_CopyingFile;
    _provider.SkippedChange -= Provider_SkippedChange;
    _provider.SkippedFileDetect -= Provider_SkippedFileDetect;
    _provider.Dispose();
     }
  }

   _provider = new FileSyncProvider(_lastFromSourceDirectory, _filter, 
    _syncOptions);
    _provider.DetectedChanges += Provider_DetectedChanges;
    _provider.ApplyingChange += Provider_ApplyingChange;
    _provider.AppliedChange += Provider_AppliedChange;
    _provider.CopyingFile += Provider_CopyingFile;
     _provider.SkippedChange += Provider_SkippedChange;
     _provider.SkippedFileDetect +=Provider_SkippedFileDetect;

     DetectChangesOnFileSystemReplica();   

     SyncFileOneWay(_fromSourceDirectory, 
                     _toLocalDirPath,_filter,_syncOptions);       
   }


    private void DetectChangesOnFileSystemReplica()
    {                     
      _provider?.DetectChanges();         
    }

    private void SyncFileOneWay(
      string sourceRootPath, string desctinationRootPath,
      FileSyncScopeFilter filter, FileSyncOptions options)
    {            
        FileSyncProvider sourceProvider = null;
        FileSyncProvider destinationProvider = null;

        try
        {

            sourceProvider = new FileSyncProvider(
                sourceRootPath, filter, options);                             

            destinationProvider = new FileSyncProvider(
                desctinationRootPath, filter, options);

            SyncOrchestrator agent = new SyncOrchestrator();
            agent.LocalProvider = destinationProvider;
            agent.RemoteProvider = sourceProvider;
            agent.Direction = SyncDirectionOrder.Download; // 
     Sync source to destination (download destination to local source)

            //agent.Direction = SyncDirectionOrder.Upload;
            var sourcePath = sourceProvider.RootDirectoryPath.TrimEnd('\');
            var destinationPath = destinationProvider.RootDirectoryPath.TrimEnd('\');                                                
            agent.Synchronize(); //sync                
        }
        finally
        {   // Release resources
            if (sourceProvider != null)
            {                   
                sourceProvider.Dispose();
            }
            if (destinationProvider != null)
            {                 
                destinationProvider.Dispose();
            }               
        }
    }

   private void Provider_DetectedChanges(object sender, DetectedChangesEventArgs e)
    {                                
     Console.WriteLine($"{nameof(e.TotalFileSize)}:{e.TotalFileSize}");
    }
 }

所以,我 运行 Sync() 每 5 分钟执行一次方法,DetectChanges() 表示它检测到变化。 然后同步。

那么,如果我不更改文件或目录,为什么方法 DetectChanges 会检测更改? 是远程目录。

我只想在远程目录确实有一些变化时才同步目录。

所以,我研究了这个问题,发现提供者在收到同步命令后,只同步需要的东西,并没有传输数据。