We need to get about 100 very small files from a remote FTP server using vb.net. Our company won't let us buy (or install) any 3rd party ftp libraries... so we are forced to use something like FtpWebRequest. (Or is there a better free, choice that is already a part of Visual Studio?)
This method works, but it is VERY slow. (I assume because of the constant logging in/out.)
Log in with user name and password. Get a file-list from the remote server. Log out Use that file-list to get each file separtely: Log in, get the file, log out. Log in 99 more times, get each file, log out each time.
Instead, we probably should be doing this, but it never works:
Log in with user name and password. ONCE. Get a list of filenames. Download each file. Log out ONCE.
We found COUNTLESS examples online of "getting an FTP file list" and later "how to download 1 file with FTP"... but we never see "get EACH file name, and download it NOW".
Dim fwr As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpSite) fwr.Credentials = New NetworkCredential(userName, password) fwr.KeepAlive = True fwr.Method = WebRequestMethods.Ftp.ListDirectory Dim sr As IO.StreamReader = Nothing Try sr = New IO.StreamReader(fwr.GetResponse().GetResponseStream()) Do Until (sr.EndOfStream()) fileName = sr.ReadLine() fwr.Method = WebRequestMethods.Ftp.DownloadFile output = "" Dim sr2 As IO.StreamReader = Nothing Try sr2 = New IO.StreamReader(fwr.GetResponse().GetResponseStream()) output = sr2.ReadToEnd() Catch ex As Exception End Try If (Not sr2 Is Nothing) Then sr2.Close() : sr2 = Nothing Call MsgBox("Got " & fileName & LF & output) Loop Catch ex As Exception End Try If (Not sr Is Nothing) Then sr.Close() : sr = Nothing If (Not fwr Is Nothing) Then fwr = Nothing
Something I just put together, the important part is the fwr.Proxy = Nothing, otherwise it tries to auto get the proxy settings which causes huge delays so setting it to nothing forces it to not use one.
If you are using a proxy obviously you need to set this to an actual proxy.
I know its a bit late but hopefully helps
Take a look at my FTP class, it might be exactly what you need.
To initialize: