ComponentPro UltimateSftp

      Display progress while transferring data

      Language Filter: AllSend comments on this topic to ComponentPro

      When the file to transfer is big and your application may take time to transfer, you may wish to show the progress of the transfer to the user. The Ultimate SFTP component provides progress notification through the Progress event. The Progress event is raised periodically while data transfer is in progress, making accessible necessary data to display progress information, such as the size of the file and the number of transferred bytes.

      The example below shows how to use the Progress event to show progress information:

      using System;
      using ComponentPro.IO;
      using ComponentPro.Net;
      
      ...
      
      [STAThread]
      static void Main()
      {
          string server = "localhost";
          string userName = "administrator";
          string password = "sfb@123";
      
          // Create a new Scp instance. 
          Scp client = new Scp();
      
          client.Progress += client_Progress;
      
          Console.WriteLine("Connecting to SCP server: {0}...", server);
      
          // Connect to the SCP server. 
          client.Connect(server);
      
          Console.WriteLine("Logging in as: {0}...", userName);
          // Authenticate. 
          client.Authenticate(userName, password);
      
          Console.WriteLine("Uploading 'test.zip'...");
      
          // Upload file "c:\test.zip". 
          client.UploadFile("c:\\test.zip", "test.zip");
      
          // And download file "test.zip". 
          Console.WriteLine("\r\nDownloading 'test.zip'...");
          client.DownloadFile("test.zip", "test_download.zip");
      
          Console.WriteLine("\n\nDisconnecting...");
      
          // Disconnect. 
          client.Disconnect();
          Console.WriteLine("Disconnected");
      }
      
      static void client_Progress(object sender, FileSystemProgressEventArgs e)
      {
          if (e.State == TransferState.Uploading)
              Console.Write("\rUploaded: {0} bytes ({1}% completed) - {2}", e.BytesTransferred, e.Percentage, e.DestinationPath);
          else if (e.State == TransferState.Downloading)
              Console.Write("\rDownloaded: {0} bytes - {2}", e.BytesTransferred, e.SourcePath);
      }