ComponentPro UltimateFtp

      QuickSynchronize(String, String, Boolean, QuickSyncOptions) Method

      See AlsoMembers Options: Show AllLanguage Filter: AllSend comments on this topic to ComponentPro
      Synchronizes the specified local directory with the specified source directory with the specified options.

      Syntax

      public void QuickSynchronize(
         string sourceDirectory, 
         string localDirectory, 
         bool sourceIsMaster, 
         QuickSyncOptions options
      )

      Parameters

      sourceDirectory
      The source directory on this file system to synchronize with the localDirectory directory.
      localDirectory
      The local directory to synchronize.
      sourceIsMaster
      A boolean value indicating whether the sourceDirectory is the master directory, meaning the content of the localDirectory will be identical to sourceDirectory's; otherwise, the content of the sourceDirectory will be identical to localDirectory's.
      options
      The QuickSyncOptions object.

      Examples

      FTP Examples

      The following example demonstrates how to handle the Progress event to report the current progress state in a Mirror operation and skip a file.

      using System;
      using ComponentPro.IO;
      using ComponentPro.Net;
      
      ...
      
      static void Main()
      {
          Ftp client = new Ftp();
      
          // Connect to the FTP server. 
          client.Connect("192.168.126.128", 21);
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          // Register event handler. 
          client.Progress += client_Progress;
      
          // Create a new instance of the MirrorOptions class. 
          QuickSyncOptions opt = new QuickSyncOptions();
      
          // Set synchronization's settings 
          // Synchronize files with different modification date time or different file size. 
          // Files with the same size and modification date time will not be synchronized. 
          opt.Comparer = FileComparers.FileLastWriteTimeComparer & FileComparers.FileSizeComparer;
          opt.Recursive = true;            
      
          // Synchronize folders. 
          client.QuickSynchronize(
              "", // Source directory path. 
              "c:\\test", // Destination directory path.                 
              true, // Source directory is master. It means "c:\test" will be identical to the remote dir. 
              opt
              );
      
      
      
          // Synchronize files with different attributes and different file size. 
          // Files with the same attributes or size will not be synchronized. 
          opt.Comparer = FileComparers.FileAttributesComparer | FileComparers.FileSizeComparer;
          opt.Recursive = true;
      
          // Synchronize folders. 
          client.QuickSynchronize(
              "my remote dir", // Source directory path. 
              "c:\\test", // Destination directory path.                 
              false, // Destination directory is master. It means remote directory "my remote dir" will be identical to the local dir "c:\test". 
              opt
              );
      
      
      
          // Synchronize files that have different contents. 
          // Files with the same content will not be synchronized. 
          opt.Comparer = FileComparers.FileContentComparer;
          opt.Recursive = true;
      
      
          // Synchronize folders. 
          client.QuickSynchronize(
              "my remote dir", // Source directory path. 
              "c:\\test", // Destination directory path.                 
              false, // Destination directory is master. It means remote directory "my remote dir" will be identical to the local dir "c:\test". 
              opt
              );
      
          // Do something here 
          // ... 
       
          client.Disconnect();
      }
      
      static void client_Progress(object sender, FileSystemProgressEventArgs e)
      {
          switch (e.State)
          {
              // For files 
              case TransferState.DeletingFile:
              case TransferState.StartUploadingFile:
              case TransferState.StartDownloadingFile:
                  Console.WriteLine(string.Format("Sync - Applying change on file '{0}', change type: {1}", e.SourceFileInfo.Name, e.State));
                  if (e.SourceFileInfo.Name == "my file")
                      e.Skip = true; // we skip the changes. 
                  break;
      
              case TransferState.FileDeleted:
              case TransferState.FileUploaded:
              case TransferState.FileDownloaded:
                  Console.WriteLine(string.Format("Sync - change applied on file '{0}', change type: {1}", e.SourceFileInfo.Name, e.State));
                  break;
      
              // For directory 
              case TransferState.DeletingDirectory:
              case TransferState.CopyingDirectory:
              case TransferState.CreatingDirectory:
                  Console.WriteLine(string.Format("Sync - Applying change on directory '{0}', change type: {1}", e.SourceFileInfo.Name, e.State));
                  if (e.SourceFileInfo.Name == "my folder")
                      e.Skip = true; // we skip the changes. 
                  break;
      
              case TransferState.DirectoryDeleted:
              case TransferState.DirectoryCreated:
                  Console.WriteLine(string.Format("Sync - change applied on directory '{0}', change type: {1}", e.SourceFileInfo.Name, e.State));
                  if (e.SourceFileInfo.Name == "my folder")
                      e.Skip = true; // we skip the changes. 
                  break;
          }
      }

      Shows how to use a custom file comparer class when synchronizing files and folders.

      using System;
      using ComponentPro.IO;
      using ComponentPro.Net;
      
      ...
      
      class CustomComparer : FileComparer
      {
          /// <summary> 
          /// Compares the target files to the master file and returns values indicating whether the target files are equal to the master. 
          /// </summary> 
          /// <param name="masterFile">The master file.</param> 
          /// <param name="targetFiles">The target files to compare.</param> 
          /// <returns><see cref="FileComparisonResult"/>.Equal if the first file is equal to second one.</returns> 
          public override FileComparisonResult[] Compare(FileInfoBase masterFile, FileInfoBase[] targetFiles)
          {
              FileComparisonResult[] results = new FileComparisonResult[targetFiles.Length];
      
              for (int i = 0; i < targetFiles.Length; i++)
              {
                  FileInfoBase targetFile = targetFiles[i];
      
                  // Two files are equal if their name, size, and last write time matched.                 
                  bool equal = string.Compare(masterFile.Name, targetFile.Name, true) == 0 &&
                               masterFile.LastWriteTime == targetFile.LastWriteTime &&
                               masterFile.Length == targetFile.Length;
      
                  results[i] = equal ? FileComparisonResult.Equal : FileComparisonResult.NotEqual;
              }
      
              return results;
          }
      }
      
      static void Main()
      {
          Ftp client = new Ftp();
      
          // Connect to the FTP server. 
          client.Connect("demo.componentpro.com");
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          // Create a new instance of the MirrorOptions class. 
          QuickSyncOptions opt = new QuickSyncOptions();
      
          // Set synchronization's settings 
          // Synchronize files with different name, modification date time, or file size. 
          opt.Comparer = new CustomComparer();
          opt.Recursive = true;
      
          // Synchronize folders. 
          client.QuickSynchronize(
              "", // Source directory path. 
              "c:\\test", // Destination directory path.                 
              true, // Source directory is master. It means "c:\test" will be identical to the remote dir. 
              opt
              );
      
          // Do something here 
          // ... 
       
          client.Disconnect();
      }

      SFTP Examples

      Shows how to use file comparer classes and synchronize files and folders.

      using System;
      using ComponentPro.IO;
      using ComponentPro.Net;
      
      ...
      
      void SynchronizeFilesDirs()
      {
          Sftp client = new Sftp();
      
          // Connect to the SFTP server. 
          client.Connect("192.168.126.128", 22);
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          // Register event handler. 
          client.Progress += client_Progress;
      
          // Create a new instance of the QuickSyncOptions class. 
          QuickSyncOptions opt = new QuickSyncOptions();
      
          // Set synchronization's settings 
          // Synchronize files with different modification date time or different file size. 
          // Files with the same size and modification date time will not be synchronized. 
          opt.Comparer = FileComparers.FileLastWriteTimeComparer & FileComparers.FileSizeComparer;
          opt.Recursive = true;
      
          // Synchronize folders. 
          client.QuickSynchronize(
              "", // Source directory path. 
              "c:\\test", // Destination directory path.                 
              true, // Source directory is master. It means "c:\test" will be identical to the remote dir. 
              opt
              );
      
      
      
          // Synchronize files with different attributes and different file size. 
          // Files with the same attributes or size will not be synchronized. 
          opt.Comparer = FileComparers.FileAttributesComparer | FileComparers.FileSizeComparer;
          opt.Recursive = true;
      
          // Synchronize folders. 
          client.QuickSynchronize(
              "my remote dir", // Source directory path. 
              "c:\\test", // Destination directory path.                 
              false, // Destination directory is master. It means remote directory "my remote dir" will be identical to the local dir "c:\test". 
              opt
              );
      
      
      
          // Synchronize files that have different contents. 
          // Files with the same content will not be synchronized. 
          opt.Comparer = FileComparers.FileContentComparer;
          opt.Recursive = true;
      
      
          // Synchronize folders. 
          client.QuickSynchronize(
              "my remote dir", // Source directory path. 
              "c:\\test", // Destination directory path.                 
              false, // Destination directory is master. It means remote directory "my remote dir" will be identical to the local dir "c:\test". 
              opt
              );
      
          // Do something here 
          // ... 
       
          client.Disconnect();
      }
      
      void client_Progress(object sender, FileSystemProgressEventArgs e)
      {
          switch (e.State)
          {
              // For files 
              case TransferState.DeletingFile:
              case TransferState.StartUploadingFile:
              case TransferState.StartDownloadingFile:
                  Console.WriteLine(string.Format("Sync - Applying change on file '{0}', change type: {1}", e.SourceFileInfo.Name, e.State));
                  if (e.SourceFileInfo.Name == "my file")
                      e.Skip = true; // we skip the changes. 
                  break;
      
              case TransferState.FileDeleted:
              case TransferState.FileUploaded:
              case TransferState.FileDownloaded:
                  Console.WriteLine(string.Format("Sync - change applied on file '{0}', change type: {1}", e.SourceFileInfo.Name, e.State));
                  break;
      
              // For directory 
              case TransferState.DeletingDirectory:
              case TransferState.CopyingDirectory:
              case TransferState.CreatingDirectory:
                  Console.WriteLine(string.Format("Sync - Applying change on directory '{0}', change type: {1}", e.SourceFileInfo.Name, e.State));
                  if (e.SourceFileInfo.Name == "my folder")
                      e.Skip = true; // we skip the changes. 
                  break;
      
              case TransferState.DirectoryDeleted:
              case TransferState.DirectoryCreated:
                  Console.WriteLine(string.Format("Sync - change applied on directory '{0}', change type: {1}", e.SourceFileInfo.Name, e.State));
                  if (e.SourceFileInfo.Name == "my folder")
                      e.Skip = true; // we skip the changes. 
                  break;
          }
      }

      Shows how to use a custom file comparer class when synchronizing files and folders.

      using ComponentPro.IO;
      using ComponentPro.Net;
      
      ...
      
      class CustomComparer : FileComparer
      {
          /// <summary> 
          /// Compares the target files to the master file and returns values indicating whether the target files are equal to the master. 
          /// </summary> 
          /// <param name="masterFile">The master file.</param> 
          /// <param name="targetFiles">The target files to compare.</param> 
          /// <returns><see cref="FileComparisonResult"/>.Equal if the first file is equal to second one.</returns> 
          public override FileComparisonResult[] Compare(FileInfoBase masterFile, FileInfoBase[] targetFiles)
          {
              FileComparisonResult[] results = new FileComparisonResult[targetFiles.Length];
      
              for (int i = 0; i < targetFiles.Length; i++)
              {
                  FileInfoBase targetFile = targetFiles[i];
      
                  // Two files are equal if their name, size, and last write time matched.                 
                  bool equal = string.Compare(masterFile.Name, targetFile.Name, true) == 0 &&
                               masterFile.LastWriteTime == targetFile.LastWriteTime &&
                               masterFile.Length == targetFile.Length;
      
                  results[i] = equal ? FileComparisonResult.Equal : FileComparisonResult.NotEqual;
              }
      
              return results;
          }
      }
      
      void Synchronize_CustomComparer()
      {
          Sftp client = new Sftp();
      
          // Connect to the SFTP server. 
          client.Connect("demo.componentpro.com");
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          // Create a new instance of the QuickSyncOptions class. 
          QuickSyncOptions opt = new QuickSyncOptions();
      
          // Set synchronization's settings 
          // Synchronize files with different name, modification date time, or file size. 
          opt.Comparer = new CustomComparer();
          opt.Recursive = true;
      
          // Synchronize folders. 
          client.QuickSynchronize(
              "", // Source directory path. 
              "c:\\test", // Destination directory path.                 
              true, // Source directory is master. It means "c:\test" will be identical to the remote dir. 
              opt
              );
      
          // Do something here 
          // ... 
       
          client.Disconnect();
      }

      ZIP Examples

      Shows how to use file comparer classes and synchronize files and folders.

      using ComponentPro.IO;
      using ComponentPro.Compression;
      
      ...
      
      void SynchronizeFilesDirs()
      {
          Zip zip = new Zip();
      
          // Open an existing archive. 
          zip.Open("test.zip", System.IO.FileMode.Open);
      
          zip.Progress += zip_Progress;
      
          // Create a new instance of the QuickSyncOptions class. 
          QuickSyncOptions opt = new QuickSyncOptions();
      
          // Set synchronization's settings 
          // Synchronize files with different modification date time or different file size. 
          // Files with the same size and modification date time will not be synchronized. 
          opt.Comparer = FileComparers.FileLastWriteTimeComparer & FileComparers.FileSizeComparer;
          opt.Recursive = true;
      
          // Synchronize folders. 
          zip.QuickSynchronize(
              "", // Source directory path within the archive. "" indicates the current directory within the archive. 
              "c:\\test", // Destination directory path.                 
              true, // Source directory is master. It means "c:\test" will be identical to the directory within the archive. 
              opt
              );
      
      
      
          // Synchronize files with different attributes and different file size. 
          // Files with the same attributes or size will not be synchronized. 
          opt.Comparer = FileComparers.FileAttributesComparer | FileComparers.FileSizeComparer;
          opt.Recursive = true;
      
          // Synchronize folders. 
          zip.QuickSynchronize(
              "/my dir", // Source directory path. 
              "c:\\test", // Destination directory path.                 
              false, // Destination directory is master. It means directory within the archive named "/my dir" will be identical to the local dir "c:\test". 
              opt
              );
      
      
      
          // Synchronize files that have different contents. 
          // Files with the same content will not be synchronized. 
          opt.Comparer = FileComparers.FileContentComparer;
          opt.Recursive = true;
      
          // Synchronize folders. 
          zip.QuickSynchronize(
              "my remote dir", // Source directory path. 
              "c:\\test", // Destination directory path.                 
              false, // Destination directory is master. It means remote directory "my remote dir" will be identical to the local dir "c:\test". 
              opt
              );
      
          // Do something here 
          // ... 
       
          zip.Close();
      }
      
      private void zip_Progress(object sender, FileSystemProgressEventArgs e)
      {
          // Skip deleting file with .doc extension. If you do not want to delete any files, always set e.Skip to true. 
          if (e.State == TransferState.DeletingFile && e.SourcePath.EndsWith(".doc"))
          {
              // Skip. 
              e.Skip = true;
          }
      }

      Shows how to use a custom file comparer class when synchronizing files and folders.

      using ComponentPro.IO;
      using ComponentPro.Compression;
      
      ...
      
      class CustomComparer : FileComparer
      {
          /// <summary> 
          /// Compares the target files to the master file and returns values indicating whether the target files are equal to the master. 
          /// </summary> 
          /// <param name="masterFile">The master file.</param> 
          /// <param name="targetFiles">The target files to compare.</param> 
          /// <returns><see cref="FileComparisonResult"/>.Equal if the first file is equal to second one.</returns> 
          public override FileComparisonResult[] Compare(FileInfoBase masterFile, FileInfoBase[] targetFiles)
          {
              FileComparisonResult[] results = new FileComparisonResult[targetFiles.Length];
      
              for (int i = 0; i < targetFiles.Length; i++)
              {
                  FileInfoBase targetFile = targetFiles[i];
      
                  // Two files are equal if their name, size, and last write time matched.                 
                  bool equal = string.Compare(masterFile.Name, targetFile.Name, true) == 0 &&
                               masterFile.LastWriteTime == targetFile.LastWriteTime &&
                               masterFile.Length == targetFile.Length;
      
                  results[i] = equal ? FileComparisonResult.Equal : FileComparisonResult.NotEqual;
              }
      
              return results;
          }
      }
      
      void Synchronize_CustomComparer()
      {
          Zip zip = new Zip();
      
          // Open an existing archive. 
          zip.Open("test.zip", System.IO.FileMode.Open);
      
          // Create a new instance of the QuickSyncOptions class. 
          QuickSyncOptions opt = new QuickSyncOptions();
      
          // Set synchronization's settings 
          // Synchronize files with different name, modification date time, or file size. 
          opt.Comparer = new CustomComparer();
          opt.Recursive = true;
      
          // Synchronize folders. 
          zip.QuickSynchronize(
              "", // Source directory path. 
              "c:\\test", // Destination directory path.                 
              true, // Source directory is master. It means "c:\test" will be identical to the specified directory within the archive. 
              opt
              );
      
          // Do something here 
          // ... 
       
          zip.Close();
      }

      Framework

      .NET Compact Framework.NET Compact Framework

      Supported version: 2.0, 3.5, and 3.9
      Assembly: ComponentPro.FileSystem.CF (in ComponentPro.FileSystem.CF.dll)

      .NET Framework.NET Framework

      Supported version: 2.0, 3.0, 3.5, 4.0, 4.5.x, 4.6.x and later
      Assembly: ComponentPro.FileSystem (in ComponentPro.FileSystem.dll)

      Portable Class Library for Windows Phone 8.1 and Windows 8.1 Store AppsPortable Class Library for Windows Phone 8.1 and Windows 8.1 Store Apps

      Supported version: 4.6.x and later
      Assembly: ComponentPro.FileSystem.WinPcl (in ComponentPro.FileSystem.WinPcl.dll)

      Universal Windows Platform (includes Windows 10 Mobile, Windows 10 Store Apps and Windows 10 IoT)Universal Windows Platform (includes Windows 10 Mobile, Windows 10 Store Apps and Windows 10 IoT)

      Supported version: 4.6.x and later
      Assembly: ComponentPro.FileSystem.Uwp (in ComponentPro.FileSystem.Uwp.dll)

      Xamarin AndroidXamarin Android

      Supported version: 2.3 and later
      Assembly: ComponentPro.FileSystem.Android (in ComponentPro.FileSystem.Android.dll)

      Xamarin MacXamarin Mac

      Supported version: 2.0.x and later
      Assembly: ComponentPro.FileSystem.Mac (in ComponentPro.FileSystem.Mac.dll)

      Xamarin iOSXamarin iOS

      Supported version: 5.1.x and later
      Assembly: ComponentPro.FileSystem.iOS (in ComponentPro.FileSystem.iOS.dll)

      See Also