Click here to Skip to main content
14,449,160 members

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

Rate this:
5.00 (11 votes)
Please Sign up or sign in to vote.
5.00 (11 votes)
7 Jul 2016CPOL
How to Use SSH.NET in Visual Studio 2015 to upload a file to an SFTP site


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


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

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))
    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:

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);


  • 7th July, 2016: Initial version


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


About the Author

United States United States
No Biography provided

Comments and Discussions

PraiseSFTP Pin
maganjo15-Apr-19 3:05
Membermaganjo15-Apr-19 3:05 
GeneralMy vote of 5 Pin
PhilippeMarcMeyer4-Apr-19 3:27
MemberPhilippeMarcMeyer4-Apr-19 3:27 
QuestionHow to do retry if initial connection fails and closed in the midway? Pin
Ch Smrutiranjan22-Jun-18 2:32
MemberCh Smrutiranjan22-Jun-18 2:32 
QuestionError with connection Pin
Member 129069616-Mar-18 15:28
MemberMember 129069616-Mar-18 15:28 
QuestionRe: Error with connection Pin
yesuraja chinta24-Sep-18 6:17
Memberyesuraja chinta24-Sep-18 6:17 
AnswerRe: Error with connection Pin
Member 1290696124-Sep-18 6:35
MemberMember 1290696124-Sep-18 6:35 
GeneralRe: Error with connection Pin
yesuraja chinta24-Sep-18 23:17
Memberyesuraja chinta24-Sep-18 23:17 
QuestionSFTP Pin
sivaram61910-Feb-17 1:21
Membersivaram61910-Feb-17 1:21 
AnswerRe: SFTP Pin
sjelen10-Feb-17 3:56
professionalsjelen10-Feb-17 3:56 
GeneralMy vote of 5 Pin
pkbrauner13-Dec-16 1:56
Memberpkbrauner13-Dec-16 1:56 
QuestionNice work. Pin
TimoKinnunen8-Jul-16 4:39
MemberTimoKinnunen8-Jul-16 4:39 
SuggestionWinSCP Assembly Pin
Kevin Straßer8-Jul-16 2:41
professionalKevin Straßer8-Jul-16 2:41 

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.

Posted 7 Jul 2016

Tagged as


24 bookmarked