How to: Get the Collection of Files in a Directory in Visual Basic. 2 minutes to read.In this articleThe overloads of the method return a read-only collection of strings representing the names of the files within a directory:.Use the overload for a simple file search in a specified directory, without searching subdirectories.Use the overload to specify additional options for your search. You can use the wildCards parameter to specify a search pattern. To include subdirectories in the search, set the searchType parameter to.An empty collection is returned if no files matching the specified pattern are found. To list files in a directory.Use one of the method overloads, supplying the name and path of the directory to search in the directory parameter. The following example returns all files in the directory and adds them to ListBox1.
First, use Try.Catch.End Try to catch the error. It helps to solve the problem. And secondly, everything you do with the web is always more or less error prone.Basic problem in your code is understanding how communication works between server and the client.
In the code above you request something from the server and the server sends you a response.I dropped the progressbar from your code because ContentLength does not return the filesize (actually reading ContentLength causes an error). You should request Ftp.GetFileSize and read the response.
Second change I made is reading incoming data in a blocks and dumping blocks to a file. Reading a large file one byte at a time can be really slow. Anyway, here's my code: Private Sub Button1Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click'Dim buffer(1023) As Byte ' Allocate a read buffer of 1kB sizeDim bytesIn As Integer ' Number of bytes read to bufferDim totalBytesIn As Integer ' Total number of bytes received (= filesize)Dim output As IO.Stream ' A file to save responseTryDim FTPRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(' & 'README.TXT'), System.Net.FtpWebRequest)' No credentials needed in this case. Usually you need to provide them.