Send and receive SFTP in .NET
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
※ It works in other versions, but it 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 will use thethird-partylibrary SSH.NET .
SSH.NET has not been updated recently, but it is relatively easy to implement the program. I don't think there are many disadvantages of using it because it also supports .NET Standard.
This time, we will send and receive files by SFTP in the .NET Core console project. SSH.NET supports many frameworks, so it can be implemented in projects other than the console.
Preparation in advance
- Visual Studio must be installed
- You have an SFTP server to verify sftp behavior
- Have an account that you can connect to with SFTP
- Enable password authentication on sftp servers when performing password authentication
- When performing public key authentication, place the public key on the SFTP server and have a private key (OpenSSH format) to be loaded into the client.
Install SSH.NET
Create a .NET Core console project in Visual Studio. The project name is SshNetBasic.
Gets the "SSH.NET" from NuGet.
The package has been added.
Password authentication
For password authentication, use the PasswordAuthenticationMethod class to set a username and password.
Set the ConnectionInfo class with a host name (such as sorceryforce.net or 192.168.0.1) and the authenticationmethod that you just generated. Pass it to the "SftpClient" class.
The SftpClient.Connect method actually makes the connection, and if it succeeds, it throws an exception to the next operation and, in case of failure. Disconnect with the Disconnect method.
// 必要な情報を設定する
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 you can see in setup tips, if you place your private key in a folder that can access other accounts, SFTP processing may fail. Be sure to make sure that you have access to the account you run or only Administrators.
What is different 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 placed.
// 必要な情報を設定する
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();
Send and receive files with SFTP
Send and receive files by writing the send and receive code after connecting to the server with the SftpClient.Connect method. What we are doing is "read local files and send them to SFTP servers" and "download files from SFTP servers and save them locally". It can be implemented without writing particularly difficult tasks.
You can send and receive japanese file names without 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 easier to send and receive SFTP. It may take longer to prepare a test environment than to implement a program.
Sftp does essentially the same thing as FTP, and most of the work is implemented in SSH.NET. This library is recommended if you want to use SFTP programmly.