Skip to content

Instantly share code, notes, and snippets.

@jorgepsmatos
Created August 22, 2019 22:47

Revisions

  1. jorgepsmatos created this gist Aug 22, 2019.
    25 changes: 25 additions & 0 deletions Download.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    public void Download(string fileToDownload, string host, string username, string password)
    {
    try
    {
    using (var sftpClient = new SftpClient(host, username, password))
    using (var fs = new FileStream(Path.GetFileName(fileToDownload), FileMode.OpenOrCreate))
    {
    sftpClient.Connect();

    sftpClient.DownloadFile(
    "/ftproot/" + fileToDownload,
    fs,
    downloaded =>
    {
    Console.WriteLine($"Downloaded {(double)downloaded / fs.Length * 100}% of the file.");
    });

    sftpClient.Disconnect();
    }
    }
    catch (Exception e)
    {
    Console.WriteLine(e.Message);
    }
    }