0

我想从具有特定搜索模式的 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');

谢谢。

4

1 回答 1

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 的搜索模式,请参见此处:

如何使用 C# 从 FTP 服务器获取一系列文件

于 2013-11-14T09:50:08.483 回答