0 votes
by (210 points)
edited

how to use the DownloadAsync Method (FileSet, String, TransferMethod, ActionOnExistingFiles, Object).

is necessary to use other methods as ConnectAsync and DisconnectAsync ?

there any example in the forum?

still can not find it ,grateful if you help me with an example

thanks

Applies to: Rebex SFTP, Rebex FTP/SSL

1 Answer

+2 votes
by (15.2k points)
edited
 
Best answer

Hi,

to use any file or directory operation on SFTP server, you need to be connected and logged to the server. You can use sftp.Connect or sftp.ConnectAsync method, then sftp.Login or sftp.LoginAsync method, which one you prefer.

Here is small sample code:

        // prepare local target directory
        string localDir = @"C:\temp\down";

        if (!Directory.Exists(localDir))
            Directory.CreateDirectory(localDir);

        // connect and login data
        string server = "test.rebex.net";
        string username = "demo";
        string password = "password";

        // connect and login to sftp server
        // using 'using' statement allows automatic dispose of Sftp client
        using (Sftp client = new Sftp())
        {
            client.Connect(server);
            client.Login(username, password);

            Task download = client.DownloadAsync(new FileSet("/", "**.txt"), localDir, TransferMethod.Copy, ActionOnExistingFiles.OverwriteOlder);

            // while the download is running do other job,
            // but make sure the download is complete 
            // before disconnecting and disposing the client
            // with .net 4.5 using 'await' key word is the best way to do this 
            while (!download.IsCompleted)
                Thread.Sleep(10);

            // now we are complete and safe to disconnect
            client.Disconnect();
        }
by (210 points)
edited

thank you very much, it was very helpful to me :)

...