Click here to Skip to main content
15,390,197 members
Articles / Programming Languages / C#
Tip/Trick
Posted 7 Jul 2016

Tagged as

Stats

148.5K views
24 bookmarked

Upload File to SFTP Site with C# in Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
7 Jul 2016CPOL1 min read
How to Use SSH.NET in Visual Studio 2015 to upload a file to an SFTP site

Introduction

This is a simple solution for uploading files to an SFTP server from .NET Framework using Visual Studio 2015.

The SSH package offers many more options that can be explored. For the purposes of this tip, I am only concentrating on a simple file transfer from a local server to an SFTP site (Upload).

Background

After searching for 2 days, I finally came across a solution out of Belgium that allowed me to programmatically upload a file to an SFTP server. There is not a lot of documentation out there on the use of SSH.Net, so I hope you find this useful. Credit to http://blog.deltacode.be/2012/01/05/uploading-a-file-using-sftp-in-c-sharp/ for the original posting of this fix. Since so many of my colleagues utilize Code Project, I felt it would be a good location to add this information.

Using the Code

In your Solution / Project:

Select Manage NuGet Packages and search for and select SSH.Net --> Install Latest Stable Version (2013.4.7) supports .NET 3.5 and 4.0

Add New Blank Class to your project - then add the following code to your class.

C#
using Renci.SshNet;
using System.IO;

namespace YourProjectNamespace
{
 class sftp
 {
  public static void UploadSFTPFile(string host, string username, 
  string password, string sourcefile, string destinationpath, int port)
  {
   using (SftpClient client = new SftpClient(host, port, username, password))
   {
    client.Connect();
    client.ChangeDirectory(destinationpath);
    using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
    {
     client.BufferSize = 4 * 1024;
     client.UploadFile(fs, Path.GetFileName(sourcefile));
    }
   }
  }
 }
}

**********************

In your event call, pass the needed parameters for your file to upload:

C#
string source = @"FilePath and FileName of Local File to Upload";
string destination = @"SFTP Server File Destination Folder";
string host = "SFTP Host";
string username = "User Name";
string password = "password";
int port = 22;  //Port 22 is defaulted for SFTP upload

sftp.UploadSFTPFile(host, username, password, source, destination, port);

History

  • 7th July, 2016: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

slccocdev
United States United States
No Biography provided

Comments and Discussions

 
QuestionAdding Event Call Pin
Member 1556325611-Mar-22 6:01
MemberMember 1556325611-Mar-22 6:01 
PraiseSFTP Pin
maganjo15-Apr-19 2:05
Membermaganjo15-Apr-19 2:05 
GeneralMy vote of 5 Pin
PhilippeMarcMeyer4-Apr-19 2:27
MemberPhilippeMarcMeyer4-Apr-19 2:27 
QuestionHow to do retry if initial connection fails and closed in the midway? Pin
Ch Smrutiranjan22-Jun-18 1:32
MemberCh Smrutiranjan22-Jun-18 1:32 
QuestionError with connection Pin
Member 129069616-Mar-18 14:28
MemberMember 129069616-Mar-18 14:28 
QuestionRe: Error with connection Pin
User 874424524-Sep-18 5:17
MemberUser 874424524-Sep-18 5:17 
AnswerRe: Error with connection Pin
Member 1290696124-Sep-18 5:35
MemberMember 1290696124-Sep-18 5:35 
GeneralRe: Error with connection Pin
User 874424524-Sep-18 22:17
MemberUser 874424524-Sep-18 22:17 
QuestionSFTP Pin
sivaram61910-Feb-17 0:21
Membersivaram61910-Feb-17 0:21 
AnswerRe: SFTP Pin
sjelen10-Feb-17 2:56
professionalsjelen10-Feb-17 2:56 
GeneralMy vote of 5 Pin
pkbrauner13-Dec-16 0:56
Memberpkbrauner13-Dec-16 0:56 
QuestionNice work. Pin
TimoKinnunen8-Jul-16 3:39
MemberTimoKinnunen8-Jul-16 3:39 
SuggestionWinSCP Assembly Pin
Kevin Straßer8-Jul-16 1:41
professionalKevin Straßer8-Jul-16 1:41 
Did you take a look at the WinSCP .NET Assembly? Works well for me Smile | :)

WinSCP .NET Assembly and COM Library :: WinSCP[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.