DetectChanges始终检测更改

时间:2019-03-04 11:56:41

标签: c# microsoft-sync-framework

我使用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}");
    }
 }

因此,我每5分钟运行一次Sync()方法,DetectChanges()说它检测到更改。 然后同步。

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

仅当远程目录确实有一些更改时,我才想同步目录。

1 个答案:

答案 0 :(得分:0)

因此,我研究了这个问题,并意识到在接收到用于同步的命令后,提供程序将同步所需的内容,而不传输数据。