Reputation: 36166
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?
Upvotes: 0
Views: 2851
Reputation: 1
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.
Upvotes: 0
Reputation: 18843
Try something like this
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
ftpRequest.Credentials = new NetworkCredential("anonymous","[email protected]");//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;
Upvotes: 2