Send and receive SFTP in .NET

Page updated :

Environment

Visual Studio
  • Visual Studio Community 2017
  • Visual Studio Community 2019
.NET Core
  • 3.1
.NET Framework
  • 4
  • 4.8
SSH.NET
  • 2016.1.0

※ Works in other versions, but is unconfirmed

At first

Create a client program for sftp to send and receive files in the .NET Framework (.NET Core). SFTP-related programs are not standard in .NET. We'll use the third-party librarySSH.NET.

SSH.NET has not been updated recently, but it is relatively easy to implement the program. Because it supports .NET Standard, I think that there are not many disadvantages by using it.

This time, sftp sends and receives files in the .NET Core console project. SSH.NET is a supported framework that can be implemented in projects other than the console.

Pre-preparation

  • Visual Studio is installed
  • You have an SFTP server to check the behavior of SFTP
  • Have an account available for sftp to connect to
  • Enable password authentication on the SFTP server when performing password authentication
  • For public key authentication, place the public key on the SFTP server and have a private key (OpenSSH format) to read to the client.

Installing SSH.NET

Create a .NET Core console project in Visual Studio. The project name is SshNetBasic.

"SSH. NET ] from NuGet.

image

image

image

image

Package added.

image

Password authentication

For password authentication, use the PasswordAuthenticationMethod class to set the user name and password.

Set the ConnectionInfo class to the host name (for example, sorceryforce.net or 192.168.0.1) and the AuthenticationMethod that you just generated. Pass to the SftpClient class.

The SftpClient.Connect method actually makes the connection, and if successful, an exception is thrown to the next operation, or if it fails. Disconnect method to disconnect.

// 必要な情報を設定する
var host = "";
var userName = "";
var password = "";

// 認証メソッドを作成
var authMethod = new Renci.SshNet.PasswordAuthenticationMethod(userName, password);

// 接続情報を作成
var connectionInfo = new Renci.SshNet.ConnectionInfo(host, userName, authMethod);

// SFTP クライアントを作成
var client = new Renci.SshNet.SftpClient(connectionInfo);

// 接続。失敗した場合は例外が発生
client.Connect();

// 切断
client.Disconnect();

Public key authentication

Place the private key in any folder on the client. As mentioned in The Tips for Setup, if you have a private key in a folder that can access other accounts, The SFTP process may fail. Make sure that you have access to the account you want to run, or anyone other than Administrators.

image

The difference from password authentication is that the authentication method has changed to "PrivateKeyAuthenticationMethod".

Specify "PrivateKeyFile" as the second argument and specify the file path and passphrase (if set) where the private key is located.

// 必要な情報を設定する
var host = "";
var userName = "";
var passPhrase = "";
var keyFilePath = @"C:\xxxxxxxxxx\id_rsa";

// 認証メソッドを作成
var authMethod = new Renci.SshNet.PrivateKeyAuthenticationMethod(userName,
                      new Renci.SshNet.PrivateKeyFile(keyFilePath, passPhrase));

// 接続情報を作成
var connectionInfo = new Renci.SshNet.ConnectionInfo(host, userName, authMethod);

// SFTP クライアントを作成
var client = new Renci.SshNet.SftpClient(connectionInfo);

// 接続。失敗した場合は例外が発生
client.Connect();

// 切断
client.Disconnect();

SFTP send and receive files

The SftpClient.Connect method connects files to the server and then writes and executes the send and receive code. What you are doing is "read the local file and send it to the SFTP server" and "Download the file from the SFTP server and save it locally" It can be implemented without writing a particularly difficult process.

Japanese file names can be sent and received without any problems.

/* ---------------- 中略 ------------------- */

var sendFilePath = @"C:\xxxxxxxxxxxx\テスト.txt";
var reseiveFilePath = @"C:\xxxxxxxxxxxx\テスト2.txt";

/* ---------------- 中略 ------------------- */

// 接続。失敗した場合は例外が発生
client.Connect();

// ファイルのアップロード(上書き)
using var sendStream = File.OpenRead(sendFilePath);
client.UploadFile(sendStream, Path.GetFileName(sendFilePath), true);

// ファイルのダウンロード(上書き)
using var reseiveStream = File.OpenWrite(reseiveFilePath);
client.DownloadFile(Path.GetFileName(sendFilePath), reseiveStream);

// 切断
client.Disconnect();

Summary

SSH.NET makes it easy to send and receive SFTP. It may take longer to set up a test environment than to implement a program.

SFTP is basically the same as FTP, and ssh.NET implements most of the work. This is a great library if you want to use SFTP programmatically.