SFTP File Upload With C# Application

Introduction

Secure file transfer protocol (SFTP) is one of the approaches to uploading to the server remotely over a secure and encrypted connection. In .NET, we can easily develop a utility to perform the mentioned task.

For this, all we need to do is to use an assembly called SSH.NET. SSH.NET is a Secure Shell (SSH) library for.NET, optimized for parallelism and with broad framework support.



We can install this library by executing the following command in package manager console.

Install-Package SSH.NET -Version 2016.0.0

Background

In order to understand the tip, basic knowledge/understanding of object-oriented programming is required.

Problem

I was given a task to upload the file to the remote server through scheduled job. I found a few articles on the internet but all of them were ambiguous, incomplete and not clear in vision. That's why I decided to write an article in the easiest way for the developer.

Why Prefer SFTP over HTTP

Although we can perform the same task using HTTP, it has drawbacks to use HTTP for file upload. HTTP is basically used for downloading the file or to upload small files on the server. In a normal scenario, Html form is used to submit the file and browser has timeout issue for a large file.

Coding Steps

Create a new project in Visual Studio.

Incorporate SSH.NET by executing the mentioned command in package manager console.

Add the following namespaces in the file. 

  1. using Renci.SshNet;  
  2. using Renci.SshNet.Sftp;   

Create an object of SftpClient and provide connection information parameter in different overload.

We can pass host, port, username, password directly into the constructor.

  1. SftpClient client = new SftpClient (host, port, username, password);  

 

OR

We can also create a connection info object and pass to sftpClient’s constructor with the public key.

  1. SftpClient sftpClient = new SftpClient (getSftpConnection ("Host""username", port, "publicKeyPath"));  
  2. public static ConnectionInfo getSftpConnection(string host, string username, int port, string publicKeyPath)  
  3. {  
  4.   return new ConnectionInfo(host, port, username, privateKeyObject(username, publicKeyPath));  
  5. }  
  6. private static AuthenticationMethod[] privateKeyObject(string username, string publicKeyPath)  
  7. {  
  8.   PrivateKeyFile privateKeyFile = new PrivateKeyFile(publicKeyPath);  
  9.   PrivateKeyAuthenticationMethod privateKeyAuthenticationMethod =  
  10.        new PrivateKeyAuthenticationMethod(username, privateKeyFile);  
  11.   return new AuthenticationMethod[] { privateKeyAuthenticationMethod };  
  12. }  
  13. Connect SftpClient.  
  14. sftpClient.Connect();   

Create an object of File Stream and pass file path.

  1. FileStream fs = new FileStream("filePath", FileMode.Open);  

 

You can set maximum buffer size in byte.

  1. sftpClient.BufferSize = 1024;  

 

Upload the file.

  1. sftpClient.UploadFile(fs, Path.GetFileName("filePath"));  

 

Dispose the object by calling dispose method of sftpClient once the file has uploaded.

  1. sftpClient.Dispose();  

 

The file has been copied to the remote location.

Completed Code 

  1. Public static void Main(string[] args)  
  2. {  
  3. Console.WriteLine("Create client Object");  
  4. using (SftpClient sftpClient = new SftpClient(getSftpConnection("host""userName", 22, "filePath")))  
  5.             {  
  6.                 Console.WriteLine("Connect to server");  
  7.                 sftpClient.Connect();  
  8.                 Console.WriteLine("Creating FileStream object to stream a file");  
  9.                 using (FileStream fs = new FileStream("filePath", FileMode.Open))  
  10.                 {  
  11.                     sftpClient.BufferSize = 1024;  
  12.                     sftpClient.UploadFile(fs, Path.GetFileName("filePath"));  
  13.                 }  
  14.                 sftpClient.Dispose();  
  15.             }  
  16. }   
  17.   
  18. public static ConnectionInfo getSftpConnection(string host, string username, int port, string publicKeyPath)  
  19. {  
  20.    return new ConnectionInfo(host, port, username, privateKeyObject(username, publicKeyPath));  
  21. }  
  22.   
  23. private static AuthenticationMethod[] privateKeyObject(string username, string publicKeyPath)  
  24. {  
  25.   PrivateKeyFile privateKeyFile = new PrivateKeyFile(publicKeyPath);  
  26.   PrivateKeyAuthenticationMethod privateKeyAuthenticationMethod =   
  27.      new PrivateKeyAuthenticationMethod(username, privateKeyFile);  
  28.       return new AuthenticationMethod[]     
  29.        {  
  30.         privateKeyAuthenticationMethod   
  31.        };  
  32. }  
Ebook Download
View all
FileInfo in C#
Read by 9.7k people
Download Now!
Learn
View all