【问题标题】:Get the list of files from FTP server with specific search pattern从具有特定搜索模式的 FTP 服务器获取文件列表
【发布时间】:2013-11-14 09:42:24
【问题描述】:

我想从具有特定搜索模式的 FTP 服务器获取文件列表(例如:获取模式为“*.txt”的所有文件)并仅使用 C#.net 下载这些文件。

以下是从FTP服务器返回文件列表的代码,请提出完成所需任务所需的附加代码。

            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + coldata.Host + "/"));

            //("ftp://" + coldata.host  + "/"));
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(coldata.Uid, coldata.Pwd);
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
            reqFTP.Timeout = System.Threading.Timeout.Infinite;
            reqFTP.Proxy = null;
            reqFTP.KeepAlive = true;
            reqFTP.UsePassive = true;
            FtpWebResponse res = (FtpWebResponse)reqFTP.GetResponse();
            response = reqFTP.GetResponse();
            reader = new StreamReader(response.GetResponseStream());
            string line = reader.ReadLine();
            while (line != null)
            {
                result.Append(line);
                result.Append("\n");
                line = reader.ReadLine();
            }
            // to remove the trailing '\n'
            result.Remove(result.ToString().LastIndexOf('\n'), 1);
            downloadRes = true;
            return result.ToString().Split('\n');

谢谢。

【问题讨论】:

    标签: c# .net c#-4.0 ftp ftpwebrequest


    【解决方案1】:

    您可以使用 System.IO.Path.GetExtension,可能是这样的:

    while (line != null)
    {
        if (System.IO.Path.GetExtension(line) == "txt")
        {
            result.Append(line);
            result.Append("\n");
            line = reader.ReadLine();
        }
    }
    

    不完全符合您的要求,但您无法指定 FTP 的搜索模式,请参见此处:

    how to fetch a range of files from an FTP server using C#

    【讨论】:

    • @code4life 那请举个例子,因为 FTP 标准不支持它,我测试的几个不同的 ftp 服务器都不起作用。
    猜你喜欢
    • 2013-03-20
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多