Created
November 29, 2017 11:54
-
-
Save lluisfranco/6d2105feabb62d9dd7f98f61f4ce208c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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