How to resolve a problem retrying to upload file that failed due to a timeout.

+1 vote
asked Oct 13, 2015 by tlaford (130 points)
edited Oct 14, 2015 by Tomas Knopp

I have downloaded a trial of Rebex and used it to build an application that runs on Windows CE 5.0.

My code tries to upload a file using ftp.PutFile(localFilename, remoteFilename) and it failed with this exception: Rebex.Net.FtpException: Timeout exceeded.

(We think the failure was because we only has "one bar".)

My code disconnects from and destroys the first FTP session, creates and connects to a new FTP session, and tries to upload again. But this time it fails with the exception : Rebex.Net.FtpException: The process cannot access the file because it is being used by another process (550).

Am I forgetting to do something when the PutFile() fails?

Can anyone offer any insight?

Applies to: Rebex FTP/SSL

1 Answer

0 votes
answered Oct 14, 2015 by Tomas Knopp (58,580 points)
edited Oct 14, 2015 by Tomas Knopp

"The process cannot access the file because it is being used by
another process (550)."

error actually comes from the FTP server and Rebex FTP client just reports it.

So according to your description it looks as though the server has the remote file still locked from the first PutFile attempt, even though you already perform the second attempt to upload the file. It might be that the server is not even aware that the first sessions has already timed out... The server still holds the lock on the file and your second attempt fails subsequently.

Luckily enough, you can work around the server problem by uploading your file under a temporary filename (e.g. "remoteFilename.tmp{attemptNumber} or any other temp filename that suits your needs") and only after the ftp.PutFile method succeeds then rename the temporary file to the desired filename using the ftp.Rename method as in the code below:

Ftp ftp = new Ftp();
ftp.Connect("server");
ftp.Login("user", "password");

string localFileName = "C:\\file.txt";
    string remoteFileName = "file.txt";

try
{
    // first try:
    string remoteTempFile1 = remoteFileName + ".tmp" + 1;
    ftp.PutFile(localFileName, remoteTempFile1);
    ftp.Rename(remoteTempFile1, remoteFileName);
    }
catch (FtpException ex)
{
    if (ex.Status == FtpExceptionStatus.Timeout)
    {

        // reconnect 
        Ftp ftp = new Ftp();
        ftp.Connect("server");
        ftp.Login("user", "password");

        // second retry in case of timeout 
        string remoteTempFile2 = remoteFileName + ".tmp" + 2;
        ftp.PutFile(localFileName, remoteTempFile2);
        ftp.Rename(remoteTempFile2, remoteFileName);
    }
}

Give it a try and let me know whether it solved this issue.

...