I use Microsoft SyncFramework 2.1.
I synchronize remote source directory with local destination directory. So, if framework detect changes - it download changs from source (remote) to destination (local).
But:
DetectChanges always detect changes, but directory not changed.
That remote directory contains 1 file.
So, i write code to synchronize it:
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}");
}
}
So, i run Sync()
method every 5 minutes and DetectChanges()
says that it detected changes.
And then it syncronize.
So, why method DetectChanges
detect changes if i do not change file or directory?
It is remote directory.
I want to synchronize directories only if remote directory really have some changes.
So, I researched this question and realized that after receiving the command for synchronization, the provider synchronizes what is needed and does not transfer the data.