当你需要获取FTP服务器上所有目录的列表时,有没有办法处理这种情况,其中目录的数量太大而得到它需要太长时间并且操作因超时而失败?
我想知道是否有一些图书馆允许你以某种方式这样做?
答案 0 :(得分:2)
尝试这样的事情
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
ftpRequest.Credentials = new NetworkCredential("anonymous","yourName@SomeDomain.com");//replace with your Creds
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();
string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();
// also add some code that will Dispose of the StreamReader object
// something like ((IDisposable)streanReader).Dispose();
// Dispose of the List<string> as well
line = null;