Download\Upload files to SFTP using SSH.NET !

.NET framework currently does not support direct (native) SFTP operations. We have to use available 3rd party libraries or components to perform these operations and SSH.NET is one of the best due to the following-

  • It can be integrated to any .NET application and used.
  • It supports SFTP functionality for both Synchronous and Asynchronous operations.
  • Supports publickey, password and keyboard-interactive authentication methods.
  • Supports RSA and DSA private key.

For more details on the features visit- GitHub link

SSH.NET is a Secure Shell (SSH) library for .NET. Below are the basic operations and respective .NET code-

  • Best practice is to store the SFTP information in config file – [example]
 <add key="SFTPHost" value="xx.xxx.xx.xxx"/> 
 <add key="SFTPPort" value="22"/>
 <add key="SFTPUsername" value="abc"/> // username
 <add key="SFTPPassword" value="password"/> //password
 <add key="SFTPFolderSource" value="from folder"/>
 <add key="SFTPFolderDest" value="to folder" />
  • Download the library and add it to the references of the project\solution. (the dll is Copyright of Renci, please refer link)
  • Add below to the class file
    using Renci.SshNet;
    using Renci.SshNet.Common;
    using Renci.SshNet.Sftp;

Operations

  • Download Files to Local from SFTP location-
 using (var sftp = new SftpClient(SFTPHost, SFTPUsername, SFTPPassword))
 {
 sftp.Connect();
 var files = sftp.ListDirectory(SFTPFolderSource);//SFTP folder from where the file is to be download
 foreach (var file in files)
 {
   string remoteFileName = file.Name;
      //LocalSource is the local folder location
      using (Stream file1 = File.OpenWrite(LocalSource + remoteFileName))
      {
      sftp.DownloadFile(SFTPFolderSource + remoteFileName, file1);
      }
 }
 }
  • Delete Files from SFTP location-
 using (var sftp = new SftpClient(SFTPHost, SFTPUsername, SFTPPassword))
 {
 sftp.Connect();
 var files = sftp.ListDirectory(SFTPFolderSource);//SFTP folder from where the file is to be download
 foreach (var file in files)
 {
   string remoteFileName = file.Name;
      //LocalSource is the local folder location
      using (Stream file1 = File.OpenWrite(LocalSource + remoteFileName))
      {
      sftp.DeleteFile(SFTPFolderSource+remoteFileName);
      }
 }
 }
  • Upload File to SFTP location from Local folder-
 using (var sftp = new SftpClient(SFTPHost, SFTPUsername, SFTPPassword))
 {
 sftp.Connect();
 sftp.CreateDirectory(SFTPFolderDest + FolderName);//Create folder if necessary else skip
 var files = Directory.GetFiles(LocalFolder);
 foreach (var file in files)
 {
    string remoteFileName = file;
    using (Stream file1 = new FileStream(file, FileMode.Open))
    {
       remoteFileName = remoteFileName.Substring(48);//Take out the filename
       sftp.UploadFile(file1, SFTPFolderDest + FolderName + "/" + remoteFileName, null);
    }
 }
 }


Thanks for checking out.

Do comment/message in case of any issues.

Please share and like.

(Reference Links- CodePlex , GitHub, Nuget.org)

Categories
Comments
All comments.
Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. jyseo

    Uploadfile generates an error if file name is Korean. filestream is not unicode?