How to upload a file to SFTP server using SharpSSH

Karl picture Karl · Nov 15, 2012 · Viewed 22.8k times · Source

I want to use SharpSSH to upload a file to a SFTP server.

I got SharpSSH.dll, the file to upload, a public key and I sent the private key to the server. They gave me a username and no password is needed.

I tried this:

Sftp sftp = new Sftp(ip, user);
sftp.Connect();
sftp.Put(filePath, toPath);
sftp.Cancel();

Do I need a HostKey somewhere here and if yes, where would I have to put it in, and how do I make one out of a .ppk file?

Answer

R Kelly picture R Kelly · Apr 10, 2014

First things first, your key terms are back-to-front, or at least I hope they are. You send the public key out, and keep the private key safe and secure.

Aside from that, yes, with SharpSSH you need to include the private key location.

sftp.AddIdentityFile("path/to/identity/file");

If your key has a password, then use the overloaded version, i.e.

sftp.AddIdentityFile("path/to/file", "password");

The key file itself, I believe, needs to be in OpenSSH format.

I'm also not sure about your inclusion of sftp.Cancel(); Would it not be better to enclose you connect and Put commands into a try/catch/finally block, and call sftp.close() within the finally block?