【问题标题】:Retrieving list of FTP file names only, without additional details仅检索 FTP 文件名列表,没有其他详细信息
【发布时间】:2015-10-09 03:26:15
【问题描述】:

我正在尝试使用 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 信息。

路径: ftp://hostname/data/resourceOrders/-rw-r--r-- 1 apache
apache 367 7 月 16 日 14:07 resource-orders-1437019656813-893.json

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

如何在没有 hacky(例如rightof())的情况下仅捕获文件名 resource-orders-1437019656813-893.json

【问题讨论】:

    标签: c# .net ftp ftpwebrequest


    【解决方案1】:

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

    【讨论】:

    • 不敢相信这么简单。非常感谢!
    【解决方案2】:

    这是我使用的函数:

    public class FileName : IComparable<FileName>
    {
        public string fName { get; set; }
        public int CompareTo(FileName other)
        {
            return fName.CompareTo(other.fName);
        }
    }
    
    public static void getFileList(string sourceURI, string sourceUser, string sourcePass, List<FileName> sourceFileList)
    {
        string line = "";
        FtpWebRequest sourceRequest;
        sourceRequest = (FtpWebRequest)WebRequest.Create(sourceURI);
        sourceRequest.Credentials = new NetworkCredential(sourceUser, sourcePass);
        sourceRequest.Method = WebRequestMethods.Ftp.ListDirectory;
        sourceRequest.UseBinary = true;
        sourceRequest.KeepAlive = false;
        sourceRequest.Timeout = -1;
        sourceRequest.UsePassive = true;
        FtpWebResponse sourceRespone = (FtpWebResponse)sourceRequest.GetResponse();
        //Creates a list(fileList) of the file names
        using (Stream responseStream = sourceRespone.GetResponseStream())
        {
            using (StreamReader reader = new StreamReader(responseStream))
            {
                line = reader.ReadLine();
                while (line != null)
                {
                    var fileName = new FileName
                            {
                                fName = line
                            };
                            sourceFileList.Add(fileName);
                    line = reader.ReadLine();
                }
            }
        }   
    }
    

    在 main() 中设置你的 sourceURI、用户和​​密码,并声明一个文件列表,如:

    List<FileName> sourceFileList = new List<FileName>();
    string sourceURI = "ftp://www.sourceftp.com/";
    string sourceUser = "testUser";
    string sourcePass = "testPass";
    

    这将为您提供一个易于迭代的文件名列表(sourceFileList[i].fName),以供您遍历!这些可以使用 .Sort() 进行排序,您也可以进行二进制搜索。

    【讨论】:

      猜你喜欢
      • 2011-12-04
      • 2017-03-06
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      • 2014-09-25
      • 2015-05-31
      • 1970-01-01
      相关资源
      最近更新 更多