This is an example of how to do multiple upload and download operation and to report status.

            
var sftp = new SftpClient("1.1.1.1", 22, "username", "password");
            sftp.Connect();

            var mem = File.OpenRead("D:\\TestFile0.bin");
            var mem1 = File.OpenRead("D:\\TestFile1.bin");
            var mem2 = File.OpenRead("D:\\TestFile2.bin");
            var mem3 = File.OpenRead("D:\\TestFile3.bin");
            var mem4 = File.OpenRead("D:\\TestFile4.bin");
            var asynch = sftp.BeginUploadFile("/home/oleg/test1.txt", mem, null, null);
            var asynch1 = sftp.BeginUploadFile("/home/oleg/test2.txt", mem1, null, null);
            var asynch2 = sftp.BeginUploadFile("/home/oleg/test3.txt", mem2, null, null);
            var asynch3 = sftp.BeginUploadFile("/home/oleg/test4.txt", mem3, null, null);
            var asynch4 = sftp.BeginUploadFile("/home/oleg/test5.txt", mem4, null, null);

            var sftpASynch = asynch as SftpAsyncResult;
            var sftpASynch1 = asynch1 as SftpAsyncResult;
            var sftpASynch2 = asynch2 as SftpAsyncResult;
            var sftpASynch3 = asynch3 as SftpAsyncResult;
            var sftpASynch4 = asynch4 as SftpAsyncResult;
            while (!sftpASynch.IsCompleted && !sftpASynch1.IsCompleted && !sftpASynch2.IsCompleted && !sftpASynch3.IsCompleted && !sftpASynch4.IsCompleted)
            {
                Console.Write(string.Format("\rUploaded {0:#########} KB Uploaded {1:#########} KB Uploaded {2:#########} KB Uploaded {3:#########} KB Uploaded {4:#########} KB", (sftpASynch.UploadedBytes / 1024), (sftpASynch1.UploadedBytes / 1024), (sftpASynch2.UploadedBytes / 1024), (sftpASynch3.UploadedBytes / 1024), (sftpASynch4.UploadedBytes / 1024)));
                Thread.Sleep(100);
            }

            sftp.EndUploadFile(asynch);
            sftp.EndUploadFile(asynch1);
            sftp.EndUploadFile(asynch2);
            sftp.EndUploadFile(asynch3);
            sftp.EndUploadFile(asynch4);

            mem = File.Create("D:\\TestFile0_out.bin");
            mem1 = File.Create("D:\\TestFile1_out.bin");
            mem2 = File.Create("D:\\TestFile2_out.bin");
            mem3 = File.Create("D:\\TestFile3_out.bin");
            mem4 = File.Create("D:\\TestFile4_out.bin");

            asynch = sftp.BeginDownload("/home/oleg/test1.txt", mem, null, null);
            asynch1 = sftp.BeginDownload("/home/oleg/test2.txt", mem1, null, null);
            asynch2 = sftp.BeginDownload("/home/oleg/test3.txt", mem2, null, null);
            asynch3 = sftp.BeginDownload("/home/oleg/test4.txt", mem3, null, null);
            asynch4 = sftp.BeginDownload("/home/oleg/test5.txt", mem4, null, null);

            sftpASynch = asynch as SftpAsyncResult;
            sftpASynch1 = asynch1 as SftpAsyncResult;
            sftpASynch2 = asynch2 as SftpAsyncResult;
            sftpASynch3 = asynch3 as SftpAsyncResult;
            sftpASynch4 = asynch4 as SftpAsyncResult;
            Console.WriteLine("Downloading");
            while (!sftpASynch.IsCompleted && !sftpASynch1.IsCompleted && !sftpASynch2.IsCompleted && !sftpASynch3.IsCompleted && !sftpASynch4.IsCompleted)
            {
                Console.Write(string.Format("\rDownloaded {0:#########} MB Downloaded {1:#########} MB Downloaded {2:#########} MB Downloaded {3:#########} MB Downloaded {4:#########} MB", (sftpASynch.DownloadedBytes / 1024), (sftpASynch1.DownloadedBytes / 1024), (sftpASynch2.DownloadedBytes / 1024), (sftpASynch3.DownloadedBytes / 1024), (sftpASynch4.DownloadedBytes / 1024)));
                Thread.Sleep(100);
            }

            sftp.EndDownload(asynch);
            sftp.EndDownload(asynch1);
            sftp.EndDownload(asynch2);
            sftp.EndDownload(asynch3);
            sftp.EndDownload(asynch4);

            mem.Close();
            mem1.Close();
            mem2.Close();
            mem3.Close();
            mem4.Close();

Last edited Dec 13, 2010 at 7:39 PM by olegkap, version 2

Comments

gregpayne Apr 15, 2016 at 5:29 PM 
When I enter this line (var sftpASynch = asynch as SftpAsyncResult;) I am getting the following error (Error 1 The type or namespace name 'SftpAsyncResult' could not be found (are you missing a using directive or an assembly reference?))

This means I can't access the DownloadedBytes and UploadedBytes property to get the status.

Is there another namespace that I need to include or could I be doing something else wrong. I am currently including the following namespaces:
using Renci.SshNet;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;

The dll version is dated 22 November 2014 (v2014.4.6.0)

Any help would be greatly appreciated.

Thanks

shankarc Aug 12, 2013 at 12:45 PM 
How can we achieve this dynamically, instead of hardcoding the instances.

tux007 Mar 12, 2013 at 1:50 AM 
This is a nice link. too.
http://sshnet.codeplex.com/wikipage?title=Draft%20for%20Documentation%20page