0 votes
by (630 points)

After using SFTP service for file download when I use File.OpenRead() I get the exception "File in use in another process".
Is there a way to flush the stream and close the file that gets downloaded?

Applies to: Rebex SFTP

1 Answer

0 votes
by (73.6k points)

Can you please describe the issue in more details?
Also a code fragment (showing what you are doing) would be very helpful.

Note that file is closed after it is downloaded automatically.

Is it possible that you called an Async method (e.g. DownloadAsync()) but you did not wait for method completion?

by (630 points)
It seems the sftp object is not released immediately after  'TransferProgressState.FileTransferred' is fired.
Here is the old pseudocode
Void ReceiveFile()
{
  _sftp.GetFile();
}

private void sftp_TransferProgressChanged(object sender, SftpTransferProgressChangedEventArgs e)
{
    if (e.TransferState == TransferProgressState.FileTransferred)
        //raise complete event
        //raise progress event           
}
       
       
I was able to fix it by changing it to

Void ReceiveFile()
{
  _sftp.GetFile();
   //raise complete event
}

private void sftp_TransferProgressChanged(object sender, SftpTransferProgressChangedEventArgs e)
{
    //raise progress event           
}
by (73.6k points)
Oh, you are right. The file is closed after the "TransferProgressState.FileTransferred" event is finished, so your workaround is the correct solution.

Note: This behavior is present in single file operations only. In such case the "TransferProgressState.FileTransferred" is kind of artificial state, because the single file operation ends almost immediately after the event.

However, if this behavior is problematic for you, we can change it in one of the future releases. Just, let us know.
...