Download, Upload files from SFTP Server in C#

SFTP is a secured file transfer protocol. We can use it in different ways. Lot of third party tools (FileZilla,WinSCP,FireFTP etc) are available for that.  Some time we need to perform basic SFTP operation in C#. This article describes step by step Download, Upload from SFTP in C#. Summary of the article:

  • What is SFTP?
  • SFTP Operations in .NET Framework
  • File List of SFTP Server in C#
  • Download File from the SFTP Server in C#
  • Upload File from Local Machine to SFTP Server in C#

 

What is SFTP?
The SFTP (Secure File Transfer Protocol) or SSH File Transfer Protocol or Secure FTP is a computing network protocol for accessing and managing files on remote computer/server/file systems. Like SCP protocol SFTP also allows file transfers between hosts. Unlike standard File Transfer Protocol (FTP), SFTP encrypt both commands and data. It prevents passwords and sensitive information from being transmitted in the clear over a network.
SFTP clients are programs that use SSH to access, transfer, and manage files. It is functionally similar to FTP clients but used different protocols. Generally we cannot use standard FTP clients to connect to SFTP servers, nor can we use clients that support only SFTP to connect to FTP servers. Some Graphical clients are available for SFTP. We can also use it from the command line on a UNIX or Mac OS X computer.

 

SFTP Operations in .NET Framework
At present .NET Framework does not support SFTP natively. We need to use a third party or open source component to do this. Some components are

  • SharpSSH is open source and a pure .NET implementation of the SSH2 client protocol suite. It provides an API for communication with SSH servers and can be integrated into any .NET application.
  • Granados is also an SSH client library for .NET.
  • Rebex SFTP is a versatile file-transfer component for .NET languages (such as C# or VB.NET) that provides secure file system access over an SSH channel using the SFTP protocol.

This article describes how to use SharpSSH in C#. SharpSSH is a free component. For this at first we need to include Tamir.SharpSSH.dll in our project. Tamir.SharpSSH dll is available in internet. We can easily download it form the web. We need to include the following namespace in the Class.

using System.Collections;
using System.Threading;
using System.IO;
using Tamir.SharpSsh;
using Tamir.Streams;

 

File List of the SFTP Server in C#
The following C# code will return the list of all the file names of a SFTP Server in an Array List.

string _ftpURL = "testsftp.com"; //Host URL or address of the SFTP server
string _UserName = "admin";     //User Name of the SFTP server
string _Password = "admin123";  //Password of the SFTP server
int _Port = 22;                 //Port No of the SFTP server (if any)
string _ftpDirectory = "Receipts"; //The directory in SFTP server where the files are present
Sftp oSftp = new Sftp(_ftpURL, _UserName, _Password);
oSftp.Connect(_Port);
ArrayList FileList = oSftp.GetFileList(_ftpDirectory);
oSftp.Close();

 

Download File from the SFTP Server in C#
The following C# code will download all the files from the SFTP server into local machine. Remember that this code can only transfer files not folder.

string _ftpURL = "testsftp.com"; //Host URL or address of the SFTP server
string _UserName = "admin";     //User Name of the SFTP server
string _Password = "admin123";  //Password of the SFTP server
int _Port = 22;                 //Port No of the SFTP server (if any)
string _ftpDirectory = "Receipts"; //The directory in SFTP server where the files are present
string LocalDirectory = "D:\\FilePuller"; //Local directory where the files will be downloaded

Sftp oSftp = new Sftp(_ftpURL, _UserName, _Password);
oSftp.Connect(_Port);
ArrayList FileList = oSftp.GetFileList(_ftpDirectory);
FileList.Remove(".");
FileList.Remove("..");          //Remove . from the file list
FileList.Remove("Processed");   //Remove folder name from the file list. If there is no folder name remove the code.

for (int i = 0; i < FileList.Count; i++)
{
    if (!File.Exists(LocalDirectory + "/" + FileList[i]))
    {
        oSftp.Get(_ftpDirectory + "/" + FileList[i], LocalDirectory + "/" + FileList[i]);
        Thread.Sleep(100);
    }
}
oSftp.Close();

 

Upload File from Local Machine to SFTP Server in C#
The following C# code will upload a file from local machine to SFTP server.

string _ftpURL = "testsftp.com"; //Host URL or address of the SFTP server
string _UserName = "admin";      //User Name of the SFTP server
string _Password = "admin123";   //Password of the SFTP server
int _Port = 22;                  //Port No of the SFTP server (if any)
string _ftpDirectory = "Receipts"; //The directory in SFTP server where the files will be uploaded
string LocalDirectory = "D:\\FilePuller"; //Local directory from where the files will be uploaded
string FileName = "test.txt";    //File name, which one will be uploaded

Sftp oSftp = new Sftp(_ftpURL, _UserName, _Password);
oSftp.Connect(_Port);
oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory + "/" + FileName);
oSftp.Close();

In this way we can download, upload files in C#. Hope you all enjoy this article. We can also Download, Upload files from FTP Server in C# .

Comments

  1. Reply

    • By Rashed

      Reply

  2. Reply

    • By Rashed

      Reply

  3. By Raghu

    Reply

  4. By Chandu

    Reply

  5. By GVaisey52

    Reply

  6. By Dine

    Reply

  7. By Ganesh

    Reply

  8. By sac

    Reply

  9. By Ismaaciil Maxamed Aadan

    Reply

  10. By Roy

    Reply

  11. By Jason

    Reply

  12. By samit

    Reply

  13. By geet

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *