Question

I've written a service for our customer that automatically transmits files to given destinations using FTP. For historic reasons I'm using WinInet to perform the FTPing. All works well, but now the customer wants to add one destination that only accepts SFTP connections.

I do not really like the idea of implementing this from scratch, so is there a way to communicate natively or through WinInet with a SFTP server? Are there any system libraries that I can use (I have no fear of P/Invoke :) )? Do I have to purchase third party components for that - if so, which ones would you suggest?

Was it helpful?

Solution

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.

OTHER TIPS

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)

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

the .NET Framwork supports FTP via the FtpWebRequest since version 2.0. No support for SFTP yet.

If you need both FTP and SFTP you have to try a third party component. There is an open source implementation of SFTP from Tamir Gal which is often recommended. Or you can try one of commercial SFTP components such as our Rebex SFTP.

Following code is taken from Rebex SFTP tutorial page:

// create client, connect and log in 
Sftp client = new Sftp();
client.Connect(hostname);
client.Login(username, password);

// upload the content of 'c:\data' directory and all subdirectories 
// to the '/wwwroot' directory at the server 
client.PutFiles(@"c:\data\*", "/wwwroot", SftpBatchTransferOptions.Recursive);

client.Disconnect();

If you are not sure whether you will need FTPS or SFTP you can check the Rebex File Transfer Pack which includes both. The FTP API is almost identical to the SFTP one.

JFYI: probably the most feature-rich components for SFTP in .NET is our SFTPBlackbox.

http://sourceforge.net/projects/sharpssh/ could be your best best. I ended up writing scripts for WinSCP though, which was easier...

EDIT for clarity, I'd recommend something like https://github.com/sshnet/SSH.NET now as SharpSSH is a dead project

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top