使用SSH.NET下载时文件为空 [英] File empty when downloaded using SSH.NET

查看:83
本文介绍了使用SSH.NET下载时文件为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SSH.NET从SFTP下载文件,这是我的代码:

I am using SSH.NET to download file from the SFTP and here is my code:

string host = ConfigurationManager.AppSettings["SFTPDomain"];
string username = ConfigurationManager.AppSettings["SFTPUser"];
string password = ConfigurationManager.AppSettings["SFTPPass"];
string remoteFileName = ConfigurationManager.AppSettings["SFTPFileName"].ToString();

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

    using (var file = File.OpenWrite(FilePath))
    {
        sftp.DownloadFile(remoteFileName, file);
    }

    sftp.Disconnect();
}

问题是下载了csv文件,但其中没有任何数据.我也更改了remoteFile路径,但仍然使用内部空数据下载了文件.我尝试使用

The problem is that the file which is csv is downloaded but it does not have any data inside. I changed the remoteFile path as well but still the file is downloaded with null data inside. I tried to check if file exists using

if (sftp.Exists(remoteFileName))
{
}

即使我用"pp"更改remoteFileName,它也始终返回true.

It always return true even if I change the remoteFileName with "pp".

谁能帮助我我做错了什么?或推荐我另一个库来从SFTP服务器下载文件.我已经尝试过WinSCP,但是遇到主机密钥错误,因此我尝试按照服务器教程的指导传递正确的SshHostKeyFingerprint .仍然出现主机密钥错误.我需要从SFTP下载文件的任何简单库吗?

Can anyone help me what I am doing wrong? Or recommend me another other library to download file from SFTP server. I have tried WinSCP but I am getting hostkey error so I tried to pass correct SshHostKeyFingerprint as guided by the server tutorial. Still I get the hostkey error. Is there any simple library i just need to download file from the SFTP?

推荐答案

我见过同样的问题.使用SSH.NET的ScpClient而不是SftpClient为我工作.可以直接替换:

I've seen the same issue. Using SSH.NET's ScpClient instead of SftpClient worked for me. It's a drop-in replacement:

using (ScpClient client = new ScpClient(host, username, password))
{
    client.Connect();

    using (Stream localFile = File.Create(localFilePath))
    {
         client.Download(remoteFilePath, localFile);
    }
}

使用ScpClient,您只能获得上载/下载功能,而不是SFTP的许多其他功能,但这对于您的用例来说可能就足够了.

With ScpClient you only get upload/download functionality instead of the many additional features of SFTP, but this may be good enough for your use-case.

这篇关于使用SSH.NET下载时文件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆