列出 FTP 目录及其子目录中的文件名

List names of files in FTP directory and its subdirectories

我在网上搜索过,没有找到任何结果。实际上,我想获取 rootDirectory 以及 Sub Directory 中所有文件的名称。我尝试了下面的代码,但它只给我 FTP.

root 中的文件

我在 FTP 中的文件夹如下所示:

/ds/product/Jan/
/ds/subproduct/Jan/
/ds/category/Jan/

我试过的代码:

FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://" + FtpIP);
ftpRequest.Credentials = new NetworkCredential(FtpUser, FtpPass);
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());

List<string> directories = new List<string>();

string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
   // directories.Add(line);
    line = streamReader.ReadLine().ToString();
    MessageBox.Show(line);
}

streamReader.Close();

如果没有任何外部库,实现起来并不容易。不幸的是,.NET Framework 和 PowerShell 都没有明确支持在 FTP 目录中递归列出文件。

您必须自己实施:

  • 列出远程目录
  • 迭代条目,递归到子目录 - 再次列出它们,等等。

棘手的部分是从子目录中识别文件。使用 .NET Framework (FtpWebRequest) 无法以可移植的方式做到这一点。不幸的是,.NET Framework 不支持 MLSD 命令,这是在 FTP 协议中检索具有文件属性的目录列表的唯一可移植方式。另见 .

您的选择是:

  • 对一个文件名进行操作,该操作肯定对文件失败而对目录成功(反之亦然)。 IE。你可以尝试下载“名字”。
  • 你可能很幸运,在你的特定情况下,你可以通过文件名从目录中区分文件(即你的所有文件都有扩展名,而子目录没有)
  • 您使用长目录列表(LIST 命令= ListDirectoryDetails 方法)并尝试解析特定于服务器的列表。许多 FTP 服务器使用 *nix 样式列表,您可以在条目的最开头通过 d 识别目录。但是许多服务器使用不同的格式。下面的例子使用了这种方法(假设是 *nix 格式)
static void ListFtpDirectory(string url, NetworkCredential credentials)
{
    WebRequest listRequest = WebRequest.Create(url);
    listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    listRequest.Credentials = credentials;

    List<string> lines = new List<string>();

    using (WebResponse listResponse = listRequest.GetResponse())
    using (Stream listStream = listResponse.GetResponseStream())
    using (StreamReader listReader = new StreamReader(listStream))
    {
        while (!listReader.EndOfStream)
        {
            string line = listReader.ReadLine();
            lines.Add(line);
        }
    }

    foreach (string line in lines)
    {
        string[] tokens =
            line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
        string name = tokens[8];
        string permissions = tokens[0];

        if (permissions[0] == 'd')
        {
            Console.WriteLine($"Directory {name}");

            string fileUrl = url + name;
            ListFtpDirectory(fileUrl + "/", credentials);
        }
        else
        {
            Console.WriteLine($"File {name}");
        }
    }
}

使用如下函数:

NetworkCredential credentials = new NetworkCredential("user", "mypassword");
string url = "ftp://ftp.example.com/directory/to/list/";
ListFtpDirectory(url, credentials);

如果您想避免解析特定于服务器的目录列表格式的麻烦,请使用支持 MLSD 命令 and/or 解析各种 LIST 列表格式的第 3 方库。

例如 WinSCP .NET assembly you can list whole directory recursively with a single call to Session.EnumerateRemoteFiles:

// Setup session options
var sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "ftp.example.com",
    UserName = "user",
    Password = "mypassword",
};

using (var session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Enumerate files
    var options =
        EnumerationOptions.EnumerateDirectories |
        EnumerationOptions.AllDirectories;
    IEnumerable<RemoteFileInfo> fileInfos =
        session.EnumerateRemoteFiles("/directory/to/list", null, options);
    foreach (var fileInfo in fileInfos)
    {
        Console.WriteLine(fileInfo.FullName);
    }
}

不仅代码更简单、更健壮且与平台无关。它还可以通过 RemoteFileInfo class.

轻松获得所有其他文件属性(大小、修改时间、权限、所有权)

如果服务器支持,WinSCP 在内部使用 MLSD 命令。如果没有,它使用 LIST 命令并支持数十种不同的列表格式。

(我是WinSCP的作者)