C# 如何确定文件是否已在SSH.NET中完成下载
我在SSH.NET/C中执行以下非常基本的任务,将文件从远程服务器下载到本地路径:C# 如何确定文件是否已在SSH.NET中完成下载,c#,ssh,sftp,C#,Ssh,Sftp,我在SSH.NET/C中执行以下非常基本的任务,将文件从远程服务器下载到本地路径: ConnectionInfo c = new PasswordConnectionInfo(remoteIP, port, username, password); var sftp = new SftpClient(c); sftp.Connect(); using (var stream = new FileStream(destinationFile, FileMode.Create)) { //down
ConnectionInfo c = new PasswordConnectionInfo(remoteIP, port, username, password);
var sftp = new SftpClient(c);
sftp.Connect();
using (var stream = new FileStream(destinationFile, FileMode.Create))
{
//download the file to our local path
sftp.DownloadFile(fileName, stream);
stream.Close();
}
sftp.Disconnect();
现在,要确定文件是否完全下载成功,是否只是代码块达到stream.Close()?或者有没有更具体的方法来确定是否一切都写得很好
编辑:如果您想查看下载了多少字节,可能会对某些人有所帮助。它还制作了一个简单的进度条,非常方便。我在帖子中测试了代码,它确实有效。查看for SSH.NET,DownloadFile()
是一个阻塞操作,在文件完全写入之前不会返回
此外,不需要在using块内部调用
stream.Close()
,因为对象将在退出块时被释放。不久前我使用SSH.NET时,出于某种原因,我不知道或不喜欢.DownloadFile没有返回值的事实。不管怎样,这就是我当时走的路线
StringBuilder sb = new StringBuilder();
ConnectionInfo c = new PasswordConnectionInfo(remoteIP, port, username, password);
var sftp = new SftpClient(c);
try
{
using (StreamReader reader = sftp.OpenText(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
sb.AppendLine(line);
}
}
File.WriteAllText(destinationFile, sb.ToString());
}
catch(Exception ex)
{
// procress exception
}
非常感谢。你真棒!