I'm using SSH.NET to implement downloading of a file via secure SSH/SFTP
connection. I need also to be able to see the progress of the download and to be able to abort it if a user wants it. So the code looks like this:
ConnectionInfo conn = new PasswordConnectionInfo(host, port, username, password);
SftpClient sshSFTP = new SftpClient(conn);
sshSFTP.Connect();
try
{
FileStream streamFile = File.Create(strLocalFilePath);
sshSFTP.DownloadFile(strFilePath, streamFile,
(ulong uiProcessedSize) =>
{
//Callback
processProgress(uiProcessedSize);
if(didUserAbortDownload())
{
//Aborted
throw new Exception("Aborted by the user");
}
});
}
catch (Exception ex)
{
Console.WriteLine("Download failed: " + ex.Message);
}
streamFile.Close();
sshSFTP.Disconnect();
I couldn't find any documented way to abort the Action
callback function for the DownloadFile
method, so I resorted to throwing an exception.
But the problem with such approach is that my custom exception is not caught.
Any idea how to remedy this?