Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload a file to SFTP server using SharpSSH

Tags:

c#

sftp

sharpssh

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?

like image 820
abc Avatar asked Mar 04 '26 18:03

abc


1 Answers

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?

like image 116
R Kelly Avatar answered Mar 07 '26 08:03

R Kelly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!