Pergunta

To download a file from a SSH Server via C#, I use Renci.SshNet. The essential code is:

try
{
    using (var sftp = new SftpClient(host, username, password))
    {
        sftp.Connect();                    

        using (var file = File.OpenWrite(localFileName))
        {
            sftp.DownloadFile(remoteFileName, file);                      }
        }
        sftp.Disconnect();
    }
}
catch (Exception ex)
{
    tbMessages.AppendText(ex.Message);
}

As the file size may be big, it takes an unpredictable time until the download has finished. But I must work with the downloaded file. How to find out, whether the download has finished or not?

Foi útil?

Solução

If this is a synchronous call, then it will only return control to the calling method when the download has completed or an exception is raised.

Looking at the call to the DownloadFile method, it does not appear to be asynchronous as there's no callback method required, therefore the rather bland answer is that it will finish when it finishes; your code will continue from that line - although you'll need to close the brace on the

using (var file = File.OpenWrite(localFileName)) 

statement!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top