Specifies a custom file comparer used to compare source and destination files to determine whether to overwrite an existing target file. It can only be used when the property FileOverwriteMode of a transfer option object was set to CustomCompare.

      Syntax

      public FileComparer Comparer { get; set; }

      Examples

      FTP Examples

      Show how to create a custom file comparer for a multiple file transfer.

      using System;
      using ComponentPro;
      using ComponentPro.Net;
      using ComponentPro.IO;
      
      ...
      
      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>A list of <see cref="FileComparisonResult"/> containing the result of the comparision.</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 and size match.                 
                  bool equal = string.Compare(masterFile.Name, targetFile.Name, true) == 0 &&
                           masterFile.Length == targetFile.Length;
      
                  results[i] = equal ? FileComparisonResult.Equal : FileComparisonResult.NotEqual;
              }
      
              return results;
          }
      }
      
      private Ftp _client;
      public void DoUpload()
      {
          // Create a new class instance. 
          Ftp client = new Ftp();
          _client = client;
      
          // Connect to the FTP server. 
          client.Connect("demo.componentpro.com");
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          // ... 
       
          client.Progress += client_Progress;
          client.TransferConfirm += client_TransferConfirm;
      
          try 
          {
              TransferOptions opt = new TransferOptions(
                  true, // BuildDirectoryTreeBeforeTransfer = true. 
                  true,
                  OptionValue.Auto,
                  new NameSearchCondition("*.exe;*.dll"),
                  FileOverwriteMode.Confirm,
                  SymlinksResolveAction.Skip);
      
              opt.Comparer = new CustomComparer();
      
              // Upload all .exe and .dll files in local folder 'c:\myfolder' to remote folder '/myfolder'. 
              // Only files with the name name and length found on the remote server are to be considered existed and need the confirmation action in the TransferConfirm event handler. 
              client.Upload("c:\\myfolder", "/myfolder", opt);
          }
          catch (Exception exc)
          {
              Console.WriteLine("Error: " + exc.Message);
          }
      
          // ... 
       
          // Disconnect. 
          client.Disconnect();
      }
      
      /// <summary> 
      /// This method is raised when user clicks on 'Cancel' button on the form. 
      /// </summary> 
      protected void btnCancel_Click(object sender, EventArgs e)
      {
          // Cancel the upload operation when user clicks on the button. 
          _client.Cancel();
      }
      
      void client_Progress(object sender, ComponentPro.IO.FileSystemProgressEventArgs e)
      {
          // Show progress info. 
          Console.WriteLine("Current File: %{0} completed", e.Percentage);
          Console.WriteLine("Total: %{0} completed", e.TotalPercentage);
      
          switch (e.State)
          {
              case TransferState.BuildingDirectoryStructure:
                  Console.WriteLine("Building directory structure...");
                  break;
      
              case TransferState.StartUploadingFile:
                  if (e.SourcePath.EndsWith(".exe"))
                  {
                      // Skip all .exe files 
                      e.Skip = true;
                  }
                  else if (e.SourcePath.StartsWith(@"C:\MyFolder"))
                  {
                      // Change the source file path if it starts with "C:\MyFolder" 
                      e.SourcePath = e.SourcePath.Replace(@"C:\MyFolder", @"C:\MySecondFolder");
                  }
                  break;
          }
      }
      
      void client_TransferConfirm(object sender, TransferConfirmEventArgs e)
      {
          if (e.Exception != null)
              Console.WriteLine("Error: " + e.Exception.Message);
      
          if (e.ConfirmReason == TransferConfirmReason.FileAlreadyExists)
          {
              // Skip the existing file. 
              e.NextAction = TransferConfirmNextActions.Skip;
          }
      
          Console.Write("Do you want to (r)etry or (s)kip?");
          string key = Console.ReadLine();
          if (key == "r")
              e.NextAction = TransferConfirmNextActions.Retry;
          else if (key == "s")
              e.NextAction = TransferConfirmNextActions.Skip;
          else 
              // Cancel the operation. 
              e.NextAction = TransferConfirmNextActions.Cancel;
      }

      SFTP Examples

      Show how to create a custom file comparer for a multiple file transfer.

      using System;
      using ComponentPro;
      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>A list of <see cref="FileComparisonResult"/> containing the result of the comparision.</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 and size match.                 
                  bool equal = string.Compare(masterFile.Name, targetFile.Name, true) == 0 &&
                           masterFile.Length == targetFile.Length;
      
                  results[i] = equal ? FileComparisonResult.Equal : FileComparisonResult.NotEqual;
              }
      
              return results;
          }
      }
      
      private Sftp _client;
      public void DoUpload()
      {
          // Create a new class instance. 
          Sftp client = new Sftp();
          _client = client;
      
          // Connect to the SFTP server. 
          client.Connect("demo.componentpro.com");
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          // ... 
       
          client.Progress += client_Progress;
          client.TransferConfirm += client_TransferConfirm;
      
          try 
          {
              TransferOptions opt = new TransferOptions(
                  true, // BuildDirectoryTreeBeforeTransfer = true. 
                  true,
                  OptionValue.Auto, 
                  new NameSearchCondition("*.exe;*.dll"),
                  FileOverwriteMode.Confirm,
                  SymlinksResolveAction.Skip);
      
              opt.Comparer = new CustomComparer();
      
              // Upload all .exe and .dll files in local folder 'c:\myfolder' to remote folder '/myfolder'. 
              // Only files with the name name and length found on the remote server are to be considered existed and need the confirmation action in the TransferConfirm event handler. 
              client.Upload("c:\\myfolder", "/myfolder", opt);
          }
          catch (Exception exc)
          {
              Console.WriteLine("Error: " + exc.Message);
          }
      
          // ... 
       
          // Disconnect. 
          client.Disconnect();
      }
      
      /// <summary> 
      /// This method is raised when user clicks on 'Cancel' button on the form. 
      /// </summary> 
      protected void btnCancel_Click(object sender, EventArgs e)
      {
          // Cancel the upload operation when user clicks on the button. 
          _client.Cancel();
      }
      
      void client_Progress(object sender, ComponentPro.IO.FileSystemProgressEventArgs e)
      {
          // Show progress info. 
          Console.WriteLine("Current File: %{0} completed", e.Percentage);
          Console.WriteLine("Total: %{0} completed", e.TotalPercentage);
      
          switch (e.State)
          {
              case TransferState.BuildingDirectoryStructure:
                  Console.WriteLine("Building directory structure...");
                  break;
      
              case TransferState.StartUploadingFile:
                  if (e.SourcePath.EndsWith(".exe"))
                  {
                      // Skip all .exe files 
                      e.Skip = true;
                  }
                  else if (e.SourcePath.StartsWith(@"C:\MyFolder"))
                  {
                      // Change the source file path if it starts with "C:\MyFolder" 
                      e.SourcePath = e.SourcePath.Replace(@"C:\MyFolder", @"C:\MySecondFolder");
                  }
                  break;
          }
      }
      
      void client_TransferConfirm(object sender, TransferConfirmEventArgs e)
      {
          if (e.Exception != null)
              Console.WriteLine("Error: " + e.Exception.Message);
      
          if (e.ConfirmReason == TransferConfirmReason.FileAlreadyExists)
          {
              // Skip the existing file. 
              e.NextAction = TransferConfirmNextActions.Skip;
          }
      
          Console.Write("Do you want to (r)etry or (s)kip?");
          string key = Console.ReadLine();
          if (key == "r")
              e.NextAction = TransferConfirmNextActions.Retry;
          else if (key == "s")
              e.NextAction = TransferConfirmNextActions.Skip;
          else 
              // Cancel the operation. 
              e.NextAction = TransferConfirmNextActions.Cancel;
      }

      ZIP Examples

      Show how to create a custom file comparer for a multiple file transfer.

      using System;
      using System.Collections.Generic;
      using System.Text;
      using ComponentPro;
      using ComponentPro.Compression;
      using ComponentPro.IO;
      using System.Windows.Forms;
      
      ...
      
      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>A list of <see cref="FileComparisonResult"/> containing the result of the comparision.</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 and size match.                 
                  bool equal = string.Compare(masterFile.Name, targetFile.Name, true) == 0 &&
                           masterFile.Length == targetFile.Length;
      
                  results[i] = equal ? FileComparisonResult.Equal : FileComparisonResult.NotEqual;
              }
      
              return results;
          }
      }
      
      private Zip _zip;
      public void DoUpload()
      {
          // Create a new instance. 
          Zip zip = new Zip();
          _zip = zip;
      
          // Open an existing archive. 
          zip.Open("test.zip");
      
          // ... 
       
          zip.Progress += zip_Progress;
          zip.TransferConfirm += zip_TransferConfirm;
      
          try 
          {
              TransferOptions opt = new TransferOptions(
                  true, // BuildDirectoryTreeBeforeTransfer = true. 
                  true,
                  OptionValue.Auto, 
                  new NameSearchCondition("*.exe;*.dll"),
                  // When FileOverwriteMode.Confirm is used the event-handler zip_TransferConfirm is invoked to let user decide whether to overwrite existing files 
                  // FileOverwriteMode.Confirm,  
                  // When FileOverwriteMode.CustomCompare is used, the Comparer will be used to compare files. 
                  FileOverwriteMode.CustomCompare,
                  SymlinksResolveAction.Skip);
      
              opt.Comparer = new CustomComparer();
      
              // Add all .exe and .dll files in local folder 'c:\myfolder' to remote folder '/myfolder'. 
              // Only files with the name name and length found on the remote server are to be considered existed and need the confirmation action in the TransferConfirm event handler. 
              zip.AddFiles("c:\\myfolder", "/myfolder", opt);
          }
          catch (Exception exc)
          {
              Console.WriteLine("Error: " + exc.Message);
          }
      
          // ... 
       
          // Close. 
          zip.Close();
      }
      
      /// <summary> 
      /// This method is raised when user clicks on 'Cancel' button on the form. 
      /// </summary> 
      protected void btnCancel_Click(object sender, EventArgs e)
      {
          // Cancel the upload operation when user clicks on the button. 
          _zip.Cancel();
      }
      
      void zip_Progress(object sender, ComponentPro.IO.FileSystemProgressEventArgs e)
      {
          // Show progress info. 
          Console.WriteLine("Current File: %{0} completed", e.Percentage);
          Console.WriteLine("Total: %{0} completed", e.TotalPercentage);
      
          switch (e.State)
          {
              case TransferState.BuildingDirectoryStructure:
                  Console.WriteLine("Building directory structure...");
                  break;
      
              case TransferState.StartUploadingFile:
                  if (e.SourcePath.EndsWith(".exe"))
                  {
                      // Skip all .exe files 
                      e.Skip = true;
                  }
                  else if (e.SourcePath.StartsWith(@"C:\MyFolder"))
                  {
                      // Change the source file path if it starts with "C:\MyFolder" 
                      e.SourcePath = e.SourcePath.Replace(@"C:\MyFolder", @"C:\MySecondFolder");
                  }
                  break;
          }
      
          // Keep the form active 
          Application.DoEvents();
      }
      
      void zip_TransferConfirm(object sender, ComponentPro.IO.TransferConfirmEventArgs e)
      {
          if (e.Exception != null)
              Console.WriteLine("Error: " + e.Exception.Message);
      
          if (e.ConfirmReason == TransferConfirmReason.FileAlreadyExists)
          {
              // Skip the existing file. 
              e.NextAction = TransferConfirmNextActions.Skip;
          }
      
          Console.Write("Do you want to (r)etry or (s)kip?");
          string key = Console.ReadLine();
          if (key == "r")
              e.NextAction = TransferConfirmNextActions.Retry;
          else if (key == "s")
              e.NextAction = TransferConfirmNextActions.Skip;
          else 
              // Cancel the operation. 
              e.NextAction = TransferConfirmNextActions.Cancel;
      }

      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