[英]WinSCP .NET assembly: How to download directories
I wrote a application in C# that uses System.IO.GetDirectoires()
and System.IO.GetFiles()
我用C#编写了一个使用
System.IO.GetDirectoires()
和System.IO.GetFiles()
的应用程序
I now have to convert that to use SFTP. 我现在必须将其转换为使用SFTP。 I have experience with
PutFiles
and GetFiles
of WinSCP .NET assembly, but I cannot figure out how to get a list of directories. 我有WinSCP .NET程序集的
PutFiles
和GetFiles
的经验,但是我不知道如何获取目录列表。 There is a GetFiles
in the winscp.exe that I can use for the files but there is no way to get the directories as far as I can tell. 我可以在文件中使用winscp.exe中的
GetFiles
,但据我所知,没有办法获取目录。 Does anyone have a way to do this or is there a library that is easier to work with. 有没有人有办法做到这一点,还是有一个更容易使用的库?
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
}
The Session.GetFiles
of WinSCP .NET assembly downloads both files and subfolders . WinSCP .NET程序集的
Session.GetFiles
下载文件和子文件夹 。
Actually, you have to explicitly specify, when you do not want to download them. 实际上,当您不想下载它们时,必须明确指定。
See How do I transfer (or synchronize) directory non-recursively? 请参阅如何以非递归方式传输(或同步)目录?
If you want to list subfolders in a remote directory, use the Session.EnumerateRemoteFiles
with EnumerationOptions.MatchDirectories
and filter the result set to the entries with the RemoteFileInfo.IsDirectory
: 如果要列出远程目录中的子文件夹,请使用带有
EnumerationOptions.MatchDirectories
的Session.EnumerateRemoteFiles
并将结果集过滤到带有RemoteFileInfo.IsDirectory
的条目中:
IEnumerable<RemoteFileInfo> directories =
session.EnumerateRemoteFiles(path, null, EnumerationOptions.MatchDirectories).
Where(file => file.IsDirectory);
But again, you do not need to do this to download the directories, the Session.GetFiles
does it for you. 但是同样,您无需执行此操作即可下载目录,
Session.GetFiles
会为您完成此操作。
Try something like that 试试这样的东西
// Connect
session.Open(sessionOptions);
RemoteDirectoryInfo directory =
session.ListDirectory("/");
foreach (RemoteFileInfo fileInfo in directory.Files)
{
Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}", fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime);
}
Also try 也试试
string dumpCommand = "ls";
session.ExecuteCommand(dumpCommand)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.