locked
Help ! How Can I perform a preview with C# SMF ? RRS feed

  • Question

  • Hello all,

    I am new to MSF.
    I wish to use it to sync files and \ or directories.
    I've already started coding what I need, and it seems to be working great.
    The desired file \ folder are synced.
    The only problem I have is - I wish to perform a check before I start syncing and preview if there is anything to sync in the desired operation - how do I do it ?
    I've seen that it is possible to do so somehow - as SyncToy v2.0 can perform PREVIEW ...

    How can I implement it ?
    Can anyone point me in the right direction please ?

    I've already gone over all the data members and attributes SyncAgent has and FileSyncProvider has with no luck.

    By the way - hooking the DetectedChangesEventArgs doesn't seem to work - as the callback function is never called , not when there are changes detected and not when there aren't any changes detected  ...

    Please someone - help !
    • Moved by Max Wang_1983 Thursday, April 21, 2011 10:15 PM forum consolidation (From:SyncFx - Technical Discussion [ReadOnly])
    Monday, February 18, 2008 4:50 PM

Answers

  • Hi,

    the following example should help.

    Basically the providers have a PreviewMode property that you can set to true. If you do that they will go through their change batch enumeration and call all events similar to a normal sync, but the actual data will not be transferred.

    The ApplyingChange event is probably most interesting to you. In my example this will output things like:
    Create/Modify... - oldName.txt -> newName.txt

    Hope that helps,
    Alex

            static void Main(string[] args)
            {
                string _sourceDir = @"c:\temp\1";
                string _destDir = @"c:\temp\2";

                SyncId _sourceId = new SyncId(new Guid("A4715EAF-341E-4ebf-90C3-71E8644CA6E8"));
                SyncId _destId = new SyncId(new Guid("FF766014-0DA4-401d-BDA1-0679326ECF96"));

                #region Scope
           
                FileSyncScopeFilter _syncFilter = new FileSyncScopeFilter();
                _syncFilter.AttributeExcludeMask = FileAttributes.Hidden | FileAttributes.System;
                _syncFilter.FileNameExcludes.Add("DoNotCopy.txt");
                _syncFilter.SubdirectoryExcludes.Add("DoNotCopy");

                FileSyncOptions _syncOptions = FileSyncOptions.RecycleDeletes | FileSyncOptions.RecycleOverwrites;
               
                #endregion

                using (FileSyncProvider _sourceProvider =
                    new FileSyncProvider(_sourceId, _sourceDir, _syncFilter, _syncOptions))
                using (FileSyncProvider _destinationProvider =
                    new FileSyncProvider(_destId, _destDir, _syncFilter, _syncOptions))
                {
                    SyncAgent _syncAgent = new SyncAgent();
                    _syncAgent.LocalProvider = _sourceProvider;
                    _syncAgent.RemoteProvider = _destinationProvider;
                    _syncAgent.Direction = SyncDirection.UploadAndDownload;
                    _destinationProvider.PreviewMode = true;
                    _destinationProvider.AppliedChange += new EventHandler<AppliedChangeEventArgs>(_destinationProvider_AppliedChange);
                    _destinationProvider.ApplyingChange += new EventHandler<ApplyingChangeEventArgs>(_destinationProvider_ApplyingChange);
                    _destinationProvider.CopyingFile += new EventHandler<CopyingFileEventArgs>(_destinationProvider_CopyingFile);
                    _destinationProvider.DetectedChanges += new EventHandler<DetectedChangesEventArgs>(_destinationProvider_DetectedChanges);
                    _destinationProvider.DetectingChanges += new EventHandler<DetectingChangesEventArgs>(_destinationProvider_DetectingChanges);
                    SyncStatistics _stats = _syncAgent.Synchronize();

                    Debug.WriteLine(string.Format("DL: {0}/{1}/{2}, UL: {3}/{4}/{5}",
                        _stats.DownloadChangesApplied.ToString(),
                        _stats.DownloadChangesFailed.ToString(),
                        _stats.DownloadChangesTotal.ToString(),
                        _stats.UploadChangesApplied.ToString(),
                        _stats.UploadChangesFailed.ToString(),
                        _stats.UploadChangesTotal.ToString()));
                }
                   
            }

            static void _destinationProvider_DetectingChanges(object sender, DetectingChangesEventArgs e)
            {
                Debug.WriteLine(e.DirectoryPath);
            }

            static void _destinationProvider_DetectedChanges(object sender, DetectedChangesEventArgs e)
            {
                Debug.WriteLine(e.TotalDirectoriesFound);
            }

            static void _destinationProvider_CopyingFile(object sender, CopyingFileEventArgs e)
            {
                Debug.WriteLine(string.Format("{0} - {1}%", e.FilePath, e.PercentCopied.ToString()));
            }

            static void _destinationProvider_ApplyingChange(object sender, ApplyingChangeEventArgs e)
            {
                Debug.WriteLine(string.Format("{0} - {1} -> {2}", e.ChangeType.ToString(), e.CurrentFileData==null?"":e.CurrentFileData.Name, e.NewFileData.Name));
            }

            static void _destinationProvider_AppliedChange(object sender, AppliedChangeEventArgs e)
            {
                Debug.WriteLine(string.Format("{0} - {1} -> {2}", e.ChangeType.ToString(), e.OldFilePath, e.NewFilePath));
            }

    Monday, February 18, 2008 9:28 PM