I'm using the following code to list the files in a directory on my FTP server. However, there are 2,914 files on the server but the following code stops after reading about 300 of them. sometimes it throws the following error:
The remote server returned an error: (450) File unavailable (e.g., file busy).
but most of the time it does not throw an error, it just stops like it is finished reading them all.
This is the code I am using.
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(new_path), FtpWebRequest)
request.UseBinary = True
request.EnableSsl = True
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails
request.Credentials = New NetworkCredential("name", "password")
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Dim responseStream As Stream = response.GetResponseStream
Dim number_of_files As Integer = 0
dirlist.Clear()
Try
Using reader As New StreamReader(responseStream)
Do While reader.Peek <> -1
Dim filename = reader.ReadLine
dirlist.Add(filename)
number_of_files += 1
Loop
End Using
Catch ex As Exception
add_to_log(log_window, "Error : " + ex.ToString)
End Try
response.Close()
add_to_log(log_window, "Files Found : " + number_of_files.ToString)
I do not understand why it is stopping before reaching the end of the list.
This is basically a VB.NET variant of FtpWebRequest ListDirectory does not return all files.
The
Peek
condition is wrong. It breaks your loop whenever there's momentarily no data ready for reading.Use this code: