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();
}