0

当您需要获取FTP服务器上所有目录的列表时,有什么方法可以处理这种情况,其中目录数量太大以至于获取它需要很长时间并且操作失败并超时?

我想知道是否有一些图书馆可以让你以某种方式做到这一点?

4

1 回答 1

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;
于 2012-02-10T15:52:06.807 回答