Although there are many Graphical Tools available for sending files to a server using SFTP. But as a developer, we may have a scenario where we need to upload a file to SFTP Serverfrom our Code.
A few days ago a job assigned to me was to develop a Task Scheduler for generating XML files daily on a specific time of the day & send these files on a Remote Server using File Transfer Protocol in a secure way.
In .Net Framework there are many Libraries available for uploading files to another machine using File Transfer Protocol but most of the libraries don’t work with .Net Core. In this Tutorial, we will develop a very simple SFTP client using C# for .Net Core.
Before start let’s have a quick look at SFTP.
What is SFTP?
SFTP stands for SSH File Transfer Protocol or Secure File Transfer Protocol. It is a protocol used to transfer files between remote machines over a secure shell.
In almost all cases, SFTP is preferable over FTP because of security features. FTP is not a secure protocol & it should only be used on a trusted network.
Choosing Library for C#
A lot of search & after testing many libraries I finally met with SSH.NET which was working perfectly with .Net Core 2.2 project & the good thing was that It does its job in a very few lines of Code.
So we’ll use SSH.NET
What is SSH.NET?
SSH.NET is an open-source library available at Nuget for .NET to work over SFTP. It is also optimized for parallelism to achieve the best possible performance. It was inspired by Sharp.SSH library which was ported from Java. This library is a complete rewrite using .Net, without any third party dependencies.
Here are the features of SSH.NET:
Creating Project
I’m in love with VS Code right after its first release so I’m going to use VS Code for creating project to upload/transfer a file to a remote server using SFTP.
Create a console application using this command
dotnet new console
Installing SSH.NET
I won’t recommend you to install the latest version of SSH.NET. It has a bug, it can be stuck on transferring the file to the remote location.
version 2016.0.0 is perfect.
run this command to install the library from NuGet
using package manager
Install-Package SSH.NET -Version 2016.0.0
or using .Net CLI
dotnet add package SSH.NET --version 2016.0.0
Code
Finally, It’s time to create a class for SFTP Client Code.
Create a file with the name as “SendFileToServer” & add the below code
using Renci.SshNet
public static class SendFileToServer
{
// Enter your host name or IP here
private static string host = "127.0.0.1";
// Enter your sftp username here
private static string username = "sftp";
// Enter your sftp password here
private static string password = "12345";
public static int Send(string fileName)
{
var connectionInfo = new ConnectionInfo(host, "sftp", new PasswordAuthenticationMethod(username, password));
// Upload File
using (var sftp = new SftpClient(connectionInfo)){
sftp.Connect();
//sftp.ChangeDirectory("/MyFolder");
using (var uplfileStream = System.IO.File.OpenRead(fileName)){
sftp.UploadFile(uplfileStream, fileName, true);
}
sftp.Disconnect();
}
return 0;
}
}
Now you can call this Method to transfer a file to SFTP Server like this
SendFileToServer.Send("myFile.txt");
“myFile.txt” is the name of the file which should be located in your project root directory.