SFTP File Upload, Copy, Delete

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 encrypts 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.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Tamir.SharpSsh;  
  6. using System.Collections;  
  7. using System.Windows.Forms;  
  8. using System.IO;  
  9. using Tamir.SharpSsh.jsch;  
  10. namespace SFTPConnectSample  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Sftp sftp = new Sftp(SFTPConnectSample.Properties.Settings.Default.HostName, SFTPConnectSample.Properties.Settings.Default.UserName, SFTPConnectSample.Properties.Settings.Default.Password);  
  17.   
  18.             sftp.Connect();  
  19.             #region Require if you want to delete Files  
  20.             JSch objjsh = new JSch();  
  21.             Session session = objjsh.getSession(SFTPConnectSample.Properties.Settings.Default.UserName, SFTPConnectSample.Properties.Settings.Default.HostName);  
  22.             // Get a UserInfo object  
  23.             UserInfo ui = new UInfo(SFTPConnectSample.Properties.Settings.Default.Password); ;  
  24.             // Pass user info to session  
  25.             session.setUserInfo(ui);  
  26.             // Open the session  
  27.             session.connect();  
  28.             Channel channel = session.openChannel("sftp");  
  29.             ChannelSftp cSftp = (ChannelSftp)channel;  
  30.             cSftp.connect();  
  31.             #endregion  
  32.             ArrayList res = new ArrayList();  
  33.              res.AddRange(sftp.GetFileList(SFTPConnectSample.Properties.Settings.Default.FromPath + "*.xml"));  
  34.              res.AddRange(sftp.GetFileList(SFTPConnectSample.Properties.Settings.Default.FromPath + "*.txt"));  
  35.              res.AddRange(sftp.GetFileList(SFTPConnectSample.Properties.Settings.Default.FromPath + "*.csv"));  
  36.             foreach (var item in res)  
  37.             {  
  38.                 if (item.ToString() != "." && item.ToString() != "..")  
  39.                 {  
  40.                     //File Copy from Remote  
  41.                     sftp.Get(SFTPConnectSample.Properties.Settings.Default.FromPath + item, Path.Combine(Application.StartupPath, SFTPConnectSample.Properties.Settings.Default.DirectoryPath + "/" + item));  
  42.                     //File Delete from Remote  
  43.                     cSftp.rm(SFTPConnectSample.Properties.Settings.Default.FromPath + item);  
  44.                     //Upload File  
  45.                     sftp.Put(Path.Combine(Path.Combine(Application.StartupPath, "XMLFiles"), item.ToString()), SFTPConnectSample.Properties.Settings.Default.ToPath + item);  
  46.                 }  
  47.   
  48.             }  
  49.             session.disconnect();  
  50.             channel.disconnect();  
  51.             cSftp.exit();  
  52.             sftp.Close();  
  53.          
  54.   
  55.   
  56.         }  
  57.     }  
  58.     public class UInfo : UserInfo  
  59.     {  
  60.   
  61.         string _passwd = string.Empty;  
  62.   
  63.         public UInfo() { _passwd = string.Empty; }  
  64.   
  65.         public UInfo(string pwd) { _passwd = pwd; }  
  66.   
  67.         public String getPassword() { return _passwd; }  
  68.   
  69.         public string Password  
  70.         {  
  71.   
  72.             set { _passwd = value; }  
  73.   
  74.             get { return _passwd; }  
  75.   
  76.         }  
  77.  
  78.         #region Dummy Implementations  
  79.   
  80.         public bool promptYesNo(String str) { return true; }  
  81.   
  82.         public String getPassphrase() { return null; }  
  83.   
  84.         public bool promptPassphrase(String message) { return true; }  
  85.   
  86.         public bool promptPassword(String message) { return true; }  
  87.   
  88.         public void showMessage(String message) { }  
  89.  
  90.         #endregion Dummy Implementations  
  91.   
  92.     }  
  93. }  
Next Recommended Reading SFTP File Upload With C# Application