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.
- using Renci.SshNet;
- 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.
- 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.
- SftpClient sftpClient = new SftpClient (getSftpConnection ("Host", "username", port, "publicKeyPath"));
- public static ConnectionInfo getSftpConnection(string host, string username, int port, string publicKeyPath)
- {
- return new ConnectionInfo(host, port, username, privateKeyObject(username, publicKeyPath));
- }
- private static AuthenticationMethod[] privateKeyObject(string username, string publicKeyPath)
- {
- PrivateKeyFile privateKeyFile = new PrivateKeyFile(publicKeyPath);
- PrivateKeyAuthenticationMethod privateKeyAuthenticationMethod =
- new PrivateKeyAuthenticationMethod(username, privateKeyFile);
- return new AuthenticationMethod[] { privateKeyAuthenticationMethod };
- }
- Connect SftpClient.
- sftpClient.Connect();
Create an object of File Stream and pass file path.
- FileStream fs = new FileStream("filePath", FileMode.Open);
You can set maximum buffer size in byte.
- sftpClient.BufferSize = 1024;
Upload the file.
- sftpClient.UploadFile(fs, Path.GetFileName("filePath"));
Dispose the object by calling dispose method of sftpClient once the file has uploaded.
- sftpClient.Dispose();
The file has been copied to the remote location.
Completed Code
- Public static void Main(string[] args)
- {
- Console.WriteLine("Create client Object");
- using (SftpClient sftpClient = new SftpClient(getSftpConnection("host", "userName", 22, "filePath")))
- {
- Console.WriteLine("Connect to server");
- sftpClient.Connect();
- Console.WriteLine("Creating FileStream object to stream a file");
- using (FileStream fs = new FileStream("filePath", FileMode.Open))
- {
- sftpClient.BufferSize = 1024;
- sftpClient.UploadFile(fs, Path.GetFileName("filePath"));
- }
- sftpClient.Dispose();
- }
- }
- public static ConnectionInfo getSftpConnection(string host, string username, int port, string publicKeyPath)
- {
- return new ConnectionInfo(host, port, username, privateKeyObject(username, publicKeyPath));
- }
- private static AuthenticationMethod[] privateKeyObject(string username, string publicKeyPath)
- {
- PrivateKeyFile privateKeyFile = new PrivateKeyFile(publicKeyPath);
- PrivateKeyAuthenticationMethod privateKeyAuthenticationMethod =
- new PrivateKeyAuthenticationMethod(username, privateKeyFile);
- return new AuthenticationMethod[]
- {
- privateKeyAuthenticationMethod
- };
- }

Anvesh JainPosted Oct 3, 2024, 7:43 AM
How to get File extension using SFTPFile class?
Sameer AnandPosted Jun 15, 2021, 8:28 AM
I am facing an issue while uploading file with longer names. I get the exception "File not found". Is there any way to resolve it?
Dennis TaylorPosted Mar 26, 2019, 2:06 PM
As usual here on C-SharpCorner. Incomplete code that makes it useless for anybody.
Harendra RavipatiPosted Oct 3, 2018, 4:57 AM
I am getting invalid private key error
Kaustubh PandeyPosted Jul 5, 2018, 11:01 PM
Which value we need to use for publicKeyPath ?
Egi AlPosted Jun 29, 2018, 6:53 AM
Is there a way to know that the file was successfully uploaded to the remote server? So I can be sure to delete that file from locally?
Prashant SinhaPosted Apr 11, 2018, 1:17 AM
How to change working directory?
Sagar BandkarPosted Mar 22, 2018, 11:57 PM
I want to upload unzip folder from local machine to remote.i can upload Single file and zip folder from local to remote.Please help me to upload unzip folder.