c# - 仅检索 FTP 文件名列表,没有其他详细信息

标签 c# .net ftp ftpwebrequest

我正在尝试使用 StackOverflow 上的这个示例将 FTP 文件夹的内容下载到本地文件夹:
Downloading a list of files from ftp to local folder using c#?

我现在的代码是:

public void DownloadFilesFromFTP(string localFilesPath, string remoteFTPPath)
{
    remoteFTPPath = "ftp://" + Hostname + remoteFTPPath;
    var request = (FtpWebRequest)WebRequest.Create(remoteFTPPath);

    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    request.Credentials = new NetworkCredential(Username, Password);
    request.Proxy = null;

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);
    List<string> directories = new List<string>();

    string line = reader.ReadLine();

    while (!string.IsNullOrEmpty(line))
    {
        directories.Add(line);
        line = reader.ReadLine();
    }
    reader.Close();

    using (WebClient ftpClient = new WebClient())
    {
        ftpClient.Credentials = new System.Net.NetworkCredential(Username, Password);

        for (int i = 0; i <= directories.Count - 1; i++)
        {
            if (directories[i].Contains("."))
            {

                string path = remoteFTPPath + @"/" + directories[i].ToString();
                string trnsfrpth = localFilesPath + @"\" + directories[i].ToString();

                ftpClient.DownloadFile(path, trnsfrpth);
            }
        }
    }

    response.Close();
}

我收到一个路径不受支持的异常,当我检查我的变量 pathtrnsfrpth 的值时,它们似乎包含 Apache 信息。

path: ftp://hostname/data/resourceOrders/-rw-r--r-- 1 apache
apache 367 Jul 16 14:07 resource-orders-1437019656813-893.json

trnsfrpth: V:\code.runner\local\orders-rw-r--r-- 1 apache apache
367 Jul 16 14:07 resource-orders-1437019656813-893.json

我如何才能只捕获文件名 resource-orders-1437019656813-893.json 而不使用 hacky(例如 rightof())方法?

最佳答案

要仅检索文件名列表而无需其他详细信息,请使用 WebRequestMethods.Ftp.ListDirectory (FTP 命令 NLST),而不是 WebRequestMethods.Ftp.ListDirectoryDetails (FTP 命令 LIST)。

关于c# - 仅检索 FTP 文件名列表,没有其他详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31509170/

相关文章:

c# - Visual Studio 2010 : How refer to a C# . 具有第三方依赖项的 Net 类库项目

.net - 使用 Windows 媒体基础的 H.264 编码

qt - Qt 4.6 中的 FTPS (FTP-SSL)

azure - 如何获取Azure数据工厂管道的静态IP?

c# - 在从 C# 代码调用 fox pro 程序方面需要帮助

c# - StringBuilder 中的 system.outofmemoryexception

c# - 测试 ViewModel PropertyChanged 事件

c# - 是否需要 bindingRedirect .config 文件或应用程序中的所有程序集?

.net - SYbase 使用 OLEDB 还是 ODBC

java - 使用java程序将数据复制到ftp时的文件大小差异