"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.