Click here to Skip to main content
13,250,182 members (56,289 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

31.6K views
18 bookmarked
Posted 7 Jul 2016

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

, 7 Jul 2016
Rate this:
Please Sign up or sign in to vote.
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.

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:

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

You may also be interested in...

Pro

Comments and Discussions

 
QuestionMessage Removed Pin
11-Jul-17 2:01
membertech360.ats11-Jul-17 2:01 
QuestionSFTP Pin
sivaram61910-Feb-17 1:21
membersivaram61910-Feb-17 1:21 
AnswerRe: SFTP Pin
sjelen10-Feb-17 3:56
membersjelen10-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 7 Jul 2016
Article Copyright 2016 by slccocdev
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid