DetectChanges始终检测更改

问题描述 投票:0回答:1

我使用Microsoft SyncFramework 2.1。

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

但:

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检测到更改?它是远程目录。

我只想在远程目录确实有一些变化的情况下同步目录。

c# microsoft-sync-framework
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.