0 votes
by (230 points)
edited

Hi, i am using the SFTP for .NET 2.0 libraries. I need to upload file to sftp server in synchronous mode. May i know the list of commands to do? Also if synchronous transfer fails, need to have some mechanism to find the failure and re-transmit it.

3 Answers

+1 vote
by (58.9k points)
selected by
 
Best answer

See list of file transfer synchronous methods available in Rebex SFTP:

for multi file operations there are these synchronous methods:

If the upload fails you will get an exception. You can retry the file transfer with the code snippet below:

        int cnt = 0;
        int maxRetry = 3;

        Sftp sftp = new Sftp();
        sftp.Connect(server);
        sftp.Login(user, password);

        while (cnt <= maxRetry)
        {
            try
            {
                sftp.PutFile(@"localPathToFile", "remotePathToFile");
                sftp.Disconnect();
                break;
            }
            catch (NetworkSessionException ex)
            {
                if (cnt == maxRetry)
                {
                        sftp.Disconnect();
                    throw;
                    }

                if (ex.Status != NetworkSessionExceptionStatus.ProtocolError)
                {
                    // reconnect needed if the status is other than ProtocolError
                    sftp.Disconnect();
                    sftp = new Sftp();
                    sftp.Connect(server);
                    sftp.Login(user, password);
                }

                cnt++;
            }
        }
    }
0 votes
by (58.9k points)
edited

Hello,

you could try the below code. It connects and authenticates to the SFTP server and then uploads one file and disconnects.

To handle errors use try catch block as in the below example:

        Sftp sftp = new Sftp();
        sftp.Connect("server");
        sftp.Login("user", "pass");

        try
        {
            sftp.PutFile("localFilePath", "remoteFilePath");
        }
        catch (NetworkSessionException ex)
        { 
            // handle exception
        }

        sftp.Disconnect();
0 votes
by (230 points)
edited

I am using Rebex SFTP.NEt 2.0. I have not found Rebex.Networking.dll in binaries. Do i need to download from Rebex website? Please advice.

by (58.9k points)
edited

Please login into your account and then download the latest Rebex SFTP version (2014R1). There are three dlls you will need Rebex.Common.dll, Rebex.Networking.dll and Rebex.Sftp.dll.

by (58.9k points)
edited

To me it looks like you are using some older version of Rebex SFTP, so please remove all old Rebex referenced libraries, and add all of the new ones as in my above answer. This should solve the problem.

...