ComponentPro UltimateSftp

      Connect to an SFTP/SSH server

      Language Filter: AllSend comments on this topic to ComponentPro

      In order to use the Sftp and other classes, you have to add references to ComponentPro.Sftp.dll, ComponentPro.Common.dll, ComponentPro.FileSystem.dll and ComponentPro.Network.dll assemblies to your project.

      Now you can use SFTP classes to connect and authenticate to the desired SFTP server.

      To authenticate to an SFTP server, perform the following steps

      1. Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
        using System;
        using ComponentPro.Net;
        
      2. Create a new instance of the Sftp class.
      3. Handle the HostKeyVerifying event to verify fingerprint if needed.
      4. Now you can connect to the SFTP server with Connect methods:
        // Create a new class instance.
        Sftp client = new Sftp();
        
        client.HostKeyVerifying += HostKeyVerifying;
        
        // Connect to the SFTP server.
        client.Connect("myserver");
        
        // Or you can specify the SFTP port with 
        // client.Connect("myserver", 22);
        
      5. Use your user name and password to login with one of Authenticate methods.
        // Authenticate.
        client.Authenticate("userName", "password");
        
      6. Do your work like browsing directories, uploading file, downloading file,etc.
        // Do something here...
        client.DownloadFile("/my remote file.dat", "my local file");
        
      7. After completing your work, call the Disconnect method to close the SFTP session. 

      Final example code

      using System;
      using ComponentPro.Net;
      
      ...
      
      public void DoConnect()
      {
          // Create a new class instance. 
          Sftp client = new Sftp();
      
          client.HostKeyVerifying += HostKeyVerifying;
      
          // Connect to the SFTP server. 
          client.Connect("myserver");
      
          // Or you can specify the SFTP port with 
          // client.Connect("myserver", 22); 
       
          // Authenticate. 
          client.Authenticate("userName", "password");
      
          // Do something here... 
          client.DownloadFile("/my remote file.dat", "my local file");
      
          // Disconnect. 
          client.Disconnect();
      }
      
      void HostKeyVerifying(object sender, HostKeyVerifyingEventArgs e)
      {
          Console.WriteLine("Host key: " + e.HostKey);
      }