How do I upload a file to an SFTP server in C# (.NET)?

C#.NetSftp

C# Problem Overview


Does a free .NET library exist with which I can upload a file to a SFTP (SSH FTP) server, which throws exceptions on problems with the upload and allows the monitoring of its progress?

C# Solutions


Solution 1 - C#

Maybe you can script/control winscp?

Update: winscp now has [a .NET library][2] available as a [nuget package][3] that supports SFTP, SCP, and FTPS

[2]: https://winscp.net/eng/docs/library "a .NET library" [3]: https://winscp.net/eng/docs/library_install#nuget "nuget pacakge"

Solution 2 - C#

Following code shows how to upload a file to a SFTP server using our Rebex SFTP component.

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

// upload the 'test.zip' file to the current directory at the server 
client.PutFile(@"c:\data\test.zip", "test.zip");

client.Disconnect();

You can write a complete communication log to a file using a LogWriter property as follows. Examples output (from FTP component but the SFTP output is similar) can be found here.

client.LogWriter = new Rebex.FileLogWriter(
   @"c:\temp\log.txt", Rebex.LogLevel.Debug); 

or intercept the communication using events as follows:

Sftp client = new Sftp();
client.CommandSent += new SftpCommandSentEventHandler(client_CommandSent);
client.ResponseRead += new SftpResponseReadEventHandler(client_ResponseRead);
client.Connect("sftp.example.org");

//... 
private void client_CommandSent(object sender, SftpCommandSentEventArgs e)
{
    Console.WriteLine("Command: {0}", e.Command);
}

private void client_ResponseRead(object sender, SftpResponseReadEventArgs e)
{
    Console.WriteLine("Response: {0}", e.Response);
}

For more info see tutorial or download a trial and check samples.

Solution 3 - C#

There is no solution for this within the .net framework.

http://www.eldos.com/sbb/sftpcompare.php outlines a list of un-free options.

your best free bet is to extend SSH using Granados. http://www.routrek.co.jp/en/product/varaterm/granados.html

Solution 4 - C#

Unfortunately, it's not in the .NET Framework itself. My wish is that you could integrate with FileZilla, but I don't think it exposes an interface. They do have scripting I think, but it won't be as clean obviously.

I've used CuteFTP in a project which does SFTP. It exposes a COM component which I created a .NET wrapper around. The catch, you'll find, is permissions. It runs beautifully under the Windows credentials which installed CuteFTP, but running under other credentials requires permissions to be set in DCOM.

Solution 5 - C#

For another un-free option try http://www.enterprisedt.com/products/edtftpnetpro/overview.html">edtFTPnet/PRO</a>;. It has comprehensive support for SFTP, and also supports FTPS (and of course FTP) if required.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCoreyView Question on Stackoverflow
Solution 1 - C#KasprzolView Answer on Stackoverflow
Solution 2 - C#Martin VobrView Answer on Stackoverflow
Solution 3 - C#ddc0660View Answer on Stackoverflow
Solution 4 - C#Mike LView Answer on Stackoverflow
Solution 5 - C#Bruce BlackshawView Answer on Stackoverflow