private static void SFTP_Connect_And_Download_Sample() | |
{ | |
var host = "sftp_server_address.com"; | |
var port = 999; | |
var username = "your_username"; | |
var passphrase = "your_passphrase"; | |
var privateKeyLocalFilePath = @"your_localpath\...\PrivateKeyOpenSSH.ppk"; | |
var remoteFolderPath = "/"; | |
var localFolferPath = @"D:\locafiles\"; | |
var key = File.ReadAllText(privateKeyLocalFilePath); | |
var buf = new MemoryStream(Encoding.UTF8.GetBytes(key)); | |
var privateKeyFile = new PrivateKeyFile(buf, passphrase); | |
var connectionInfo = new ConnectionInfo(host, port, username, | |
new PrivateKeyAuthenticationMethod(username, privateKeyFile)); | |
using (var client = new SftpClient(connectionInfo)) | |
{ | |
client.Connect(); | |
var files = client.ListDirectory(remoteFolderPath); | |
foreach (var file in files) | |
{ | |
Console.WriteLine(file); | |
using (var targetFile = new FileStream(Path.Combine(localFolferPath, file.Name), FileMode.Create)) | |
{ | |
client.DownloadFile(file.FullName, targetFile); | |
targetFile.Close(); | |
} | |
} | |
client.Disconnect(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment