FtpWebRequest ListDirectory does not return all fi

2019-05-15 08:15发布

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();
}

1条回答
女痞
2楼-- · 2019-05-15 08:52

The Peek() condition is wrong. It breaks your loop whenever there's momentarily no data ready for reading.

Use this code:

string line;
while (!string.IsNullOrEmpty(line = listReader.ReadLine()))
{
    w.WriteLine(line);
}

Though if you just need to copy the stream, use this:

w.Write(listReader.ReadToEnd());

Or even better (more efficient):

using (Stream fileStream = File.Create("test.txt"))
{
    responseListStream.CopyTo(fileStream);
}
查看更多
登录 后发表回答