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.
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
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Tamir.SharpSsh;
- using System.Collections;
- using System.Windows.Forms;
- using System.IO;
- using Tamir.SharpSsh.jsch;
- namespace SFTPConnectSample
- {
- class Program
- {
- static void Main(string[] args)
- {
- Sftp sftp = new Sftp(SFTPConnectSample.Properties.Settings.Default.HostName, SFTPConnectSample.Properties.Settings.Default.UserName, SFTPConnectSample.Properties.Settings.Default.Password);
- sftp.Connect();
- #region Require if you want to delete Files
- JSch objjsh = new JSch();
- Session session = objjsh.getSession(SFTPConnectSample.Properties.Settings.Default.UserName, SFTPConnectSample.Properties.Settings.Default.HostName);
- // Get a UserInfo object
- UserInfo ui = new UInfo(SFTPConnectSample.Properties.Settings.Default.Password); ;
- // Pass user info to session
- session.setUserInfo(ui);
- // Open the session
- session.connect();
- Channel channel = session.openChannel("sftp");
- ChannelSftp cSftp = (ChannelSftp)channel;
- cSftp.connect();
- #endregion
- ArrayList res = new ArrayList();
- res.AddRange(sftp.GetFileList(SFTPConnectSample.Properties.Settings.Default.FromPath + "*.xml"));
- res.AddRange(sftp.GetFileList(SFTPConnectSample.Properties.Settings.Default.FromPath + "*.txt"));
- res.AddRange(sftp.GetFileList(SFTPConnectSample.Properties.Settings.Default.FromPath + "*.csv"));
- foreach (var item in res)
- {
- if (item.ToString() != "." && item.ToString() != "..")
- {
- //File Copy from Remote
- sftp.Get(SFTPConnectSample.Properties.Settings.Default.FromPath + item, Path.Combine(Application.StartupPath, SFTPConnectSample.Properties.Settings.Default.DirectoryPath + "/" + item));
- //File Delete from Remote
- cSftp.rm(SFTPConnectSample.Properties.Settings.Default.FromPath + item);
- //Upload File
- sftp.Put(Path.Combine(Path.Combine(Application.StartupPath, "XMLFiles"), item.ToString()), SFTPConnectSample.Properties.Settings.Default.ToPath + item);
- }
- }
- session.disconnect();
- channel.disconnect();
- cSftp.exit();
- sftp.Close();
- }
- }
- public class UInfo : UserInfo
- {
- string _passwd = string.Empty;
- public UInfo() { _passwd = string.Empty; }
- public UInfo(string pwd) { _passwd = pwd; }
- public String getPassword() { return _passwd; }
- public string Password
- {
- set { _passwd = value; }
- get { return _passwd; }
- }
- #region Dummy Implementations
- public bool promptYesNo(String str) { return true; }
- public String getPassphrase() { return null; }
- public bool promptPassphrase(String message) { return true; }
- public bool promptPassword(String message) { return true; }
- public void showMessage(String message) { }
- #endregion Dummy Implementations
- }
- }

kuba rebiszPosted Oct 12, 2021, 1:38 PM
Came in handy, thanks!
Balachandra RaddiPosted Aug 3, 2021, 12:27 PM
I have downloaded your code. SFTP upload and download is working fine but I am trying to add additional features like keeping backup but I am not able to check the directory exists check condition here to create the directory. Getting an exception as "Object reference not set to an instance of an object." Here is my code: if (sDownArchive == "Yes") { String Todaysdate = DateTime.Now.ToString("dd-MM-yyyy"); //Need to add condition here to check directory exists to avoid exception sftp.Mkdir(sDownArchivePath+"/"+Todaysdate); cSftp.rename(sDownFrom + item, Path.Combine(sDownArchivePath+"/"+Todaysdate+"/"+item)); WriteToDownLog(" Archived file '" + item.ToString() + "' To " + sDownArchivePath); } Please help me in this case.
prabu pmpPosted Jul 1, 2021, 1:26 PM
An unhandled exception of type 'Tamir.SharpSsh.jsch.JSchException' occurred in Tamir.SharpSSH.dll , Additional information: Algorithm negotiation fail
GIRISH GPosted Apr 5, 2018, 8:00 AM
Hi Ankush, Any idea how to move files from SFTP to sharepoint library?
Nilesh BukeliaPosted Jul 25, 2017, 7:19 AM
Works fine! GREAT.
Ankush AgnihotriPosted Apr 6, 2017, 5:27 AM
Please update the below 3 lines if someone getting error related to path sftp.Get(SFTPIntegration.Properties.Settings.Default.FromPath + "/" + item, Path.Combine(Application.StartupPath, SFTPIntegration.Properties.Settings.Default.DirectoryPath + @"\" + item)); //Upload File in local //lstFiles.Add(Path.Combine(Application.StartupPath, SFTPIntegration.Properties.Settings.Default.DirectoryPath + @"\" + item)); //upload file on InProgess folder sftp.Put(Path.Combine(Application.StartupPath, SFTPIntegration.Properties.Settings.Default.DirectoryPath + @"\" + item), SFTPIntegration.Properties.Settings.Default.InProgressPath + "/" + item); //File Delete from Remote cSftp.rm(SFTPIntegration.Properties.Settings.Default.FromPath + "/" + item);