Skip to content

Instantly share code, notes, and snippets.

@lluisfranco
Created November 29, 2017 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lluisfranco/6d2105feabb62d9dd7f98f61f4ce208c to your computer and use it in GitHub Desktop.
Save lluisfranco/6d2105feabb62d9dd7f98f61f4ce208c to your computer and use it in GitHub Desktop.
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