SFTPテスト (アップロード) (SSH.NET) 2016/11
ソースコード
ソースコード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Renci.SshNet;
namespace ConsoleSFTP2
{
class Program
{
static void Main(string[] args)
{
// サーバURL (IPアドレスも可"
//string url = "hoge.kd2.jp";
string url = "xxx.xxx.xxx.xxx";
// サーバポート
int port = 22;
// ユーザ
string username = "sshuser";
// パスワード
string password = "sshpassword";
// SSHセッション情報
ConnectionInfo CInfo = new ConnectionInfo(url, port, username,
new PasswordAuthenticationMethod[] {
new PasswordAuthenticationMethod(username, password)
}
);
string localPath = "path\to\localfile";
// SFTPクライアント
using (SftpClient sftpClient = new SftpClient(CInfo))
{
sftpClient.Connect();
using (var file = System.IO.File.OpenRead(localPath))
{
sftpClient.ChangeDirectory("/tmp");
sftpClient.UploadFile(file, localPath, true);
}
sftpClient.Disconnect();
}
}
}
}