How To Connect With SFTP Using WinSCP

To connect and transfer files to FTP there is a Class available called FTP in .net under System.IO and System.Net namespaces. However to connect and transfer files on SFTP, there is no inbuilt class available in .Net.

WinSCP is the third party open-source client which gives the facility to connect and transfer files on SFTP.

Using the below steps you can connect with SFTP

Step 1

Install WinSCP exe and connect it with your credential details, so that you can check the actual files and folders.

Step 2

Install library using npm with below command, it will add dll file in your solution

Install-Package WinSCP -Version 5.13.4

Step 3

Include namespace in your page/class where you want to write code as below

using WinSCP;

Step 4

With the help of the below code you can connect and access SFTP.

  1. SessionOptions sessionOptions = new SessionOptions();  
  2. sessionOptions.Protocol = Protocol.Sftp;  
  3. sessionOptions.HostName = ftpurl;  
  4. sessionOptions.UserName = ftpusername;  
  5. sessionOptions.Password = ftppassword;  
  6. sessionOptions.SshHostKeyFingerprint = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";  
  7. Session session = new Session();  
  8. session.Open(sessionOptions);  
  9. TransferOptions transferOptions = new TransferOptions();  
  10. transferOptions.TransferMode = TransferMode.Binary;  
  11. TransferOperationResult transferResult;  
  12. //This is for Getting/Downloading files from SFTP  
  13. transferResult = session.GetFiles(DirectoryPath, destinationFtpUrl, false, transferOptions);  
  14. //This is for Putting/Uploading file on SFTP  
  15. transferResult = session.PutFiles(DirectoryPath, destinationFtpUrl, false, transferOptions);  
  16. transferResult.Check();  
  17. }  
Connect With SFTP Using WinSCP

 Connect With SFTP Using WinSCP

 

Here SshHostKeyFingerprint can be gotten from WinSCP. Please refer to the below images to get SshHostKeyFingerprint. It is marked in the red box. Note that every SFTP has a different SshHostKeyFingerprint.

Blue Ocktopus Technology Systems Private Limited
Enabling revenue generation through brand loyalty and customer retention for enterprise retail and e