I am trying to retrieve the list of files from a FTP location which has about 9000 files.
But the following code always gives only 97 files. In the beginning of the loop for the 98th file, the StreamReader.Peek()
turns to -1
The output "test.txt" always has only the first 97 files, as in, the FTP response itself contains only 97 files.
Appreciate any help.
requestList = (FtpWebRequest)WebRequest.Create("xxx");
requestList.Credentials = new NetworkCredential("xx", "xx");
requestList.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
responseList = (FtpWebResponse)requestList.GetResponse();
responseListStream = responseList.GetResponseStream();
listReader = new StreamReader(responseListStream);
using (StreamWriter w = new StreamWriter("test.txt"))
{
while (listReader.Peek() >= 0)
{
w.WriteLine(listReader.ReadLine());
}
w.Close();
}
The
Peek()
condition is wrong. It breaks your loop whenever there's momentarily no data ready for reading.Use this code:
Though if you just need to copy the stream, use this:
Or even better (more efficient):