0 votes
ago by (170 points)

Below is the code which I am trying to achieve the requirement (using Upload method of FileTransferClient class).

Triying to trasfert file between servers without downloading at aplication side

 private void btnSendFile_Click(object sender, EventArgs e)
    {
        try
        {
            FileTransferClient client = new FileTransferClient();
            client.TransferProgressChanged += new EventHandler<TransferProgressChangedEventArgs>(TransferProgressChanged);
            client.Connect("Server", 22, FileTransferMode.Sftp);
            if (client.IsConnected == true)
            {
                client.Login("AccountName", "AccountPassword");
                if (client.IsAuthenticated == true)
                {
                    client.Upload("/Server1/home/file.txt", "/Server2/home/Downloads");
                }
            }
        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message);
        }
    }
    void TransferProgressChanged(object sender, TransferProgressChangedEventArgs e)
    {
        //do your operation
    }
Applies to: Rebex SFTP

1 Answer

0 votes
ago by (150k points)

Your code should work. Are you having any problems with it?
In that case, use FileTransferClient's logging API to create a debug log, which should make it possible to tell what's going on.

Some general suggestions that could be useful:
- IsConnectes will always return true after a successful Connect call, so there is no need to check it.
- IsAuthenticated will always return true after a successful Login call, so there is no need to check it either.
- Using synchronous variants of FileTransferClient methods in a click event handler is not a good idea, because it would block the application. Depending on the application type (is it a web app or a Windows app?), consider using asynchronous variants, or run the whole operation in a background thread.
- It's a good practice to disconnect or dispose the instance of FileTransferClient when it's no longer needed. Otherwise, the connection would stay open for a while until the instance is claimed by .NET's garbage collector.
- There is no need to register TransferProgressChanged if the handler is empty.

private async void btnSendFile_Click(object sender, EventArgs e)
{
    try
    {
        using (var client = new FileTransferClient())
        {
            await client.ConnectAsync("test.rebex.net", 22, FileTransferMode.Sftp);
            await client.LoginAsync("demo", "password");
            await client.UploadAsync("/Server1/home/file.txt", "/Server2/home/Downloads");
        }
    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.Message);
    }
}
ago by (170 points)
Yes, I am getting error as "Could not find source path ('D:\root\testfile.exe')" as UploadAsync method does not have any parameter to specify the source file system (source client object).

My current line of ode looks like this

 using (var client = new FileTransferClient())
        {
            await client.ConnectAsync("test.rebex.net", 22, FileTransferMode.Sftp);
            await client.LoginAsync("demo", "password");
            await client.UploadAsync("/root/testfile.exe", "/root/Desktop", TraversalMode.Recursive, TransferMethod.Copy,ActionOnExistingFiles.ThrowException);
        }
ago by (150k points)
Upload/UploadAsync method does actually accept a normal Windows path with a drive letter, just like common .NET methods such as File.OpenRead(...).

For example:
    await client.UploadAsync(@"D:\root\testfile.exe", "/root/Desktop", TraversalMode.Recursive, TransferMethod.Copy,ActionOnExistingFiles.ThrowException);

Alternatively, you can even use forward slashes instead of backslashes:
    await client.UploadAsync("D:/root/testfile.exe", "/root/Desktop", TraversalMode.Recursive, TransferMethod.Copy,ActionOnExistingFiles.ThrowException);
...