How to communicate with SFTP server

There's no support for SFTP in .NET framework, in any version.


You have to use a third party library for SFTP.

You can use WinSCP .NET assembly. There's even a WinSCP NuGet package.

A trivial SFTP upload C# example:

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
    SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Upload files
    session.PutFiles(@"d:\toupload\*", "/home/user/").Check();
}

There are lot of other examples.


You can have WinSCP GUI generate an SFTP code template, like above, for you, including C#, VB.NET and PowerShell.

enter image description here


The assembly is just a wrapper around WinSCP scripting, so it's not a completely native .NET code. As such it does not fit all use cases in .NET framework. It is mostly suitable for automating tasks, somewhat for developing GUI applications, and not really for web applications.

For a fully native .NET SFTP library, see SSH.NET, which is strangely not mentioned in any answer yet.

(I'm the author of WinSCP)


Windows 10 also come with command-line OpenSSH sftp client. It can also be downloaded for older versions of Windows.


I created a demo of interactions with SFTP server using WinSCP.

This application is able to upload, delete, rename and fetch info of files system in an SFTP Server using WinSCP .NET Assembly.

Take a look at: https://github.com/ducfilan/SFTP-with-WinSCP


Unfortunately, SFTP is not natively supported by WinInet or any other standard Windows libraries.

I've had good luck with /n Software's IP*Works SSH for .NET in talking to a large variety of SFTP servers.

Tags:

C#

.Net

Sftp