Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
private static void SFTP_Connect_And_Download_Sample()
var host = "";
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))
var files = client.ListDirectory(remoteFolderPath);
foreach (var file in files)
using (var targetFile = new FileStream(Path.Combine(localFolferPath, file.Name), FileMode.Create))
client.DownloadFile(file.FullName, targetFile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.