0

Is there any way to deal with situation when you need to get list of all directories on a FTP server, where the number of directories is so big that it takes too long to get it and operation fails with timeout?

I wonder if there are some libraries that let you do that somehow?

iLemming
  • 34,477
  • 60
  • 195
  • 309
  • Can you limit the data i.e only find the first 5 levels and do a LOD methodology? – Brad Semrad Feb 10 '12 at 15:49
  • can you get it in chunks? Say all folder that start with 'a', then another query for those which start with 'b', etc. Perhaps you can split the query in other ways as well (ie date) – Adrian Feb 10 '12 at 15:49
  • The key is ListDirectory I posted an example below of how you can do it.. thanks Happy Friday – MethodMan Feb 10 '12 at 15:53

2 Answers2

2

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;
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • it gets stuck on (FtpWebResponse)ftpRequest.GetResponse() – iLemming Feb 10 '12 at 16:08
  • what do you mean when you say it gets stuck..please provide more information also can you wrap your code around a Try Catch.. – MethodMan Feb 10 '12 at 16:10
  • well if you have a lot of Directories to loop thru this could be normal.. also it's hard to tell not knowing what type of Network / Connection you have .. there could be so many possibilities.. but the fact that the code works is a Plus.. – MethodMan Feb 10 '12 at 16:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7536/discussion-between-dj-kraze-and-agzam) – MethodMan Feb 10 '12 at 16:23
  • you could change the code to look like something here but it's pretty similar to what I have the only difference is the While !EndOfStream in this code located here... http://sharpertutorials.com/ultimate-guide-ftp/ check out the full code sample .. only you will be able to determine if you can utilize its functionality or not.. let me know – MethodMan Feb 10 '12 at 16:27
  • 1
    better yet.. here is an example from MSDN where you can do it Async http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx I would really like to help you solve this issue I have never had that issue because of the Directory structure of where I am is not that large.. – MethodMan Feb 10 '12 at 16:54
0

In this case, the account used by your code to List the directory may be different from the account which was used to create the directory.

Due to this, access privilege issue may occur eventually resulting in a timeout.

Please verify a single account is used for directory creation/Listing.

For example, Consider a Directory named '2012' and there are 2 sub-directories inside this namely 'JAN' and 'FEB'. Assume, 'JAN' was created programmatically and 'FEB' was created manually. Now, if you try to List 'FEB' directory from your program, the code may timeout due to which you feel like it is stuck.