Ftp directory listing timeout. A huge number of subdirs

Is there a way to cope with a situation where you need to get a list of all the directories on an FTP server, where the number of directories is so large that it takes too much time to get it, and the operation has timed out?

I wonder if there are some libraries that will allow you to do this?

0
source share
1 answer

Try something like this

        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;
+2
source

All Articles