使用 SSH.NET 库从 SFTP 下载文件 [英] Download files from SFTP with SSH.NET library

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

问题描述

string host = @"ftphost";
string username = "user";
string password = "********";
string localFileName  = System.IO.Path.GetFileName(@"localfilename");
string remoteDirectory = "/export/";
using (var sftp = new SftpClient(host, username, password))
{
    sftp.Connect();
    var files = sftp.ListDirectory(remoteDirectory);
    foreach (var file in files)
    {
        if (!file.Name.StartsWith("."))
        {
            string remoteFileName = file.Name;
            if (file.LastWriteTime.Date == DateTime.Today)

            Console.WriteLine(file.FullName);

            File.OpenWrite(localFileName);

            string sDir = @"localpath";

            Stream file1 = File.OpenRead(remoteDirectory + file.Name);
            sftp.DownloadFile(remoteDirectory, file1);
        }
    }
}

我使用 SSH.NET (Renci.SshNet) 库来处理 SFTP 服务器.我需要做的是根据今天的日期从 SFTP 服务器上的特定文件夹中抓取文件.然后将这些文件从 SFTP 服务器复制到我的服务器的本地驱动器.

I am using SSH.NET (Renci.SshNet) library to work with an SFTP server. What I need to do is grab files from a specific folder on the SFTP server based on today's date. Then copy those files from the SFTP server to a local drive a server of mine.

上面是我的代码,但它不起作用.有时它说文件不存在,但有时我要下载的文件不会在我的本地服务器上,但我需要下载当天上传到远程文件夹的任何文件.

Above is the code I have but it is not working. Sometimes it says file does not exist but sometimes the files I will be downloading will not be on my local servers but I need to download whatever files were uploaded to the remote folder for that day.

有人可以看看有什么问题吗?我相信这与流部分有关.除了上传文件之外,我还使用了 FTP,我使用了一些以前的代码,并认为我可以逆转该过程,但这不起作用.我使用的代码基于此示例.>

Can someone take a look and see what is wrong? I believe it has something to do with the stream portion. I have worked with FTP much besides uploading files which I took some previous code I had and thought I could reverse the process but that isn't working. The code I used is based off of this example.

推荐答案

使用 SSH.NET 库下载文件的简单工作代码是:

A simple working code to download a file with SSH.NET library is:

using (Stream fileStream = File.Create(@"C:	argetlocalpathfile.zip"))
{
    sftp.DownloadFile("/source/remote/path/file.zip", fileStream);
}

另请参阅在 C# 中使用 SSH.NET SFTP 下载目录.

解释一下,为什么你的代码不起作用:

SftpClient.DownloadFile 的第二个参数是写入下载内容的流.

The second parameter of SftpClient.DownloadFile is a stream to write a downloaded contents to.

您传入的是读流而不是写流.此外,您打开读取流的路径是远程路径,不能与仅对本地文件进行操作的 File 类一起使用.

You are passing in a read stream instead of a write stream. And moreover the path you are opening read stream with is a remote path, what cannot work with File class operating on local files only.

只需丢弃 File.OpenRead 行并使用先前 File.OpenWrite 调用的结果(您现在根本没有使用):

Just discard the File.OpenRead line and use a result of previous File.OpenWrite call instead (that you are not using at all now):

Stream file1 = File.OpenWrite(localFileName);

sftp.DownloadFile(file.FullName, file1);

或者更好的是,使用 File.Create 丢弃本地文件可能具有的任何先前内容.

Or even better, use File.Create to discard any previous contents that the local file may have.

我不确定您的 localFileName 是否应该包含完整路径,或者只是文件名.因此,如有必要,您可能还需要添加路径(将 localFileNamesDir 组合?)

I'm not sure if your localFileName is supposed to hold full path, or just file name. So you may need to add a path too, if necessary (combine localFileName with sDir?)

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

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