ComponentPro UltimateSftp

      Upload files simultaneously

      Language Filter: AllSend comments on this topic to ComponentPro

      Uploading files and directories from the local disk to the remote SFTP server is quite simple. All that needs to occur is passing appropriate server information such as hostname, port, username, password/private key and the number of threads to the Upload method of the Sftp class.

      The following steps illustrate how to use the Upload to upload files and directories.

      using System;
      using System.Threading;
      using ComponentPro.IO;
      using ComponentPro.Net;
      static void Main()
          // Create a new class instance. 
          Sftp client = new Sftp();
          // Connect to the server. 
          // Authenticate. 
          client.Authenticate("user", "pass");
          client.CommandResponse += client_ResponseRead;
          // You can register the ThreadStateChanged event here or in the client_Progress event handler as shown below. 
          client.ThreadStateChanged += Thread_StateChanged; 
          client.Progress += client_Progress;
          // ... 
          // Upload files and subdirectories from "c:\my folder" to "/my folder" using 3 threads. This waits untils these threads complete. 
          client.Upload("c:\\my folder", "/my folder", 3, true);
          // ... 
      static void client_Progress(object sender, FileSystemProgressEventArgs e)
          // You can access the threads used for the multi-thread file operation here. 
          // Uncomment the following code to register the StateChanged event of each thread. 
          //if (e.State == TransferState.DirectoryStructureBuilt) 
          //    for (int i = 0; i < e.TransferStatistics.Threads.Count; i++) 
          //    { 
          //        e.TransferStatistics.Threads[i].StateChanged += Thread_StateChanged; 
          //    } 
          // When the multi-thread operation completes the status is MultiFileOperationCompleted. 
          if (e.State == TransferState.MultiFileOperationCompleted)
              Console.WriteLine("File transfer completed");
      static long _threadsCompleted;
      static void Thread_StateChanged(object sender, TransferThreadStateChangedEventArgs e)
          if (e.State == TransferThreadState.Stopped) // The thread is stopped 
              TransferThreadInfo info = (TransferThreadInfo)sender;
              Console.WriteLine(string.Format("Thread ID {0} completed", info.ThreadId));
              Interlocked.Increment(ref _threadsCompleted);
              if (Interlocked.Read(ref _threadsCompleted) == 3)
                  Console.WriteLine("Multi-threads file transfer completed");
      static void client_ResponseRead(object sender, CommandResponseEventArgs e)
          Sftp client = (Sftp)sender;
          if (client.ThreadId >= 0)
              if (e.Command != null)
                  Console.WriteLine("Thread: {0} - CMD>       {1}", client.ThreadId,
                  Console.WriteLine("Thread: {0} - RESPONSE>  {1}", client.ThreadId,