使用SSH.NET库从SFTP下载文件 [英] Download files from SFTP with SSH.NET library
问题描述
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:\target\local\path\file.zip"))
{
sftp.DownloadFile("/source/remote/path/file.zip", fileStream);
}
See also Downloading a directory using SSH.NET SFTP in C#.
为了解释,为什么您的代码无法正常工作:
SftpClient.DownloadFile $ c $的第二个参数c>是用于将下载内容写入其中的流。
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 $的结果c $ c>调用(您现在根本没有使用):
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
应该包含完整路径,或仅包含文件名。因此,如有必要,您可能还需要添加路径(将 localFileName
与 sDir
组合?)
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屋!