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