C# does not have native capabilities to use SFTP, but fortunately there are libraries available to do this. Most of these libraries are commercial libraries, but there are two that are open-source and free to use.

These are SharpSSh by a guy called Tamir Gal, and Granados.

SharpSSH is a relatively easy library to use, and allows either a straight SSH connection, or provides a wrapper to transfer files.

The code to send a file to a remote server is rather easy using this library.

Sftp scp = new Sftp(HostName, SSHUsername);
scp.Password = SSHPassword;

scp.Connect(SSHPort);
scp.Put(fullFileName, remoteName);
scp.Close();

Don’t forget to include the reference to the library

using Tamir.SharpSsh;

The library does provide a lot more functionality that you can play around with, for example, you can check if the connection is connected or not, and check what cipher you are using. You can also use this object to create folders on the remote server, and download files from the server too.

Share