Transfer Files To 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. To download please use the link below.

https://winscp.net/eng/download.php 

Step 2

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

PM> Install-Package WinSCP -Version 5.13.4

Step 3

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

usingWinSCP;

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();  

Here you can get SshHostKeyFingerprint 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.

 
 
Next Recommended Reading SFTP File Upload With C# Application

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