SSH.NET SFTP递归获取目录和文件列表 [英] SSH.NET SFTP Get a list of directories and files recursively

查看:710
本文介绍了SSH.NET SFTP递归获取目录和文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Renci.SshNet库通过使用SFTP递归获取文件和目录的列表.我可以连接SFTP站点,但是我不确定如何在C#中递归获取目录和文件列表.我还没有找到任何有用的例子.

I am using Renci.SshNet library to get a list of files and directories recursively by using SFTP. I can able to connect SFTP site but I am not sure how to get a list of directories and files recursively in C#. I haven't found any useful examples.

有人尝试过吗?如果是这样,您是否可以发布一些示例代码来说明如何递归获取这些文件和文件夹.

Has anybody tried this? If so, can you post some sample code about how to get these files and folders recursively.

谢谢,
Prav

Thanks,
Prav

推荐答案

此库中有一些怪癖,使得此递归列表比较棘手,因为ChangeDirectoryListDirectory之间的交互无法正常工作.

This library has some quirks that make this recursive listing tricky because the interaction between the ChangeDirectory and ListDirectory do not work as you may expect.

以下内容未列出/home目录中的文件,而是列出了/(根)目录中的文件:

The following does not list the files in the /home directory instead it lists the files in the / (root) directory:

sftp.ChangeDirectory("home");
sftp.ListDirectory("").Select (s => s.FullName);

以下内容无效不起作用,并返回SftpPathNotFoundException:

The following does not work and returns a SftpPathNotFoundException:

sftp.ChangeDirectory("home");
sftp.ListDirectory("home").Select (s => s.FullName);

以下是列出/home目录中文件的正确方法

The following is the correct way to list the files in the /home directory

sftp.ChangeDirectory("/");
sftp.ListDirectory("home").Select (s => s.FullName);

如果你问我,这真是太疯狂了.除非您在此方法的参数中指定文件夹,否则使用ChangeDirectory方法设置默认目录对ListDirectory方法无效.似乎应该为此编写一个错误.

This is pretty crazy if you ask me. Setting the default directory with the ChangeDirectory method has no effect on the ListDirectory method unless you specify a folder in the parameter of this method. Seems like a bug should be written for this.

因此,编写递归函数时,必须设置一次默认目录,然后在遍历文件夹的过程中在ListDirectory调用中更改目录.该清单返回一个可枚举的SftpFiles.然后可以分别检查IsDirectory == true.请注意,清单还返回了...条目(它们是目录).如果要避免无限循环,则需要跳过这些内容. :-)

So when you write your recursive function you'll have to set the default directory once and then change the directory in the ListDirectory call as you iterate over the folders. The listing returns an enumerable of SftpFiles. These can then be checked individually for IsDirectory == true. Just be aware that the listing also returns the . and .. entries (which are directories). You'll want to skip these if you want to avoid an infinite loop. :-)

编辑2/23/2018

EDIT 2/23/2018

我正在查看我的一些旧答案,并为上面的答案表示歉意,并提供以下工作代码.请注意,此示例不需要ChangeDirectory,因为它对ListDirectory使用Fullname:

I was reviewing some of my old answers and would like to apologize for the answer above and supply the following working code. Note that this example does not require ChangeDirectory, since it's using the Fullname for the ListDirectory:

void Main()
{
    using (var client = new Renci.SshNet.SftpClient("sftp.host.com", "user", "password"))
    {
        var files = new List<String>();
        client.Connect();
        ListDirectory(client, ".", ref files);
        client.Disconnect();
        files.Dump();
    }
}

void ListDirectory(SftpClient client, String dirName, ref List<String> files)
{
    foreach (var entry in client.ListDirectory(dirName))
    {

        if (entry.IsDirectory)
        {
            ListDirectory(client, entry.FullName, ref files);
        }
        else
        {
            files.Add(entry.FullName);
        }
    }
}

这篇关于SSH.NET SFTP递归获取目录和文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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