【发布时间】:2018-04-09 16:48:11
【问题描述】:
我正在使用SSH.NET 通过安全的SSH/SFTP 连接实现文件下载。我还需要能够看到下载的进度,并能够在用户需要时中止它。所以代码看起来像这样:
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();
我找不到任何记录方法来中止 DownloadFile 方法的 Action 回调函数,所以我求助于抛出异常。
但是这种方法的问题是我的自定义异常没有被捕获。
知道如何解决这个问题吗?
【问题讨论】: