Usando o FTP para baixar cada arquivo * ENQUANTO * obtendo a lista de arquivos
Precisamos obter cerca de 100 arquivos muito pequenos de um servidor FTP remoto usando vb.net. Nossa empresa não nos permite comprar (ou instalar) bibliotecas ftp de terceiros ... por isso somos forçados a usar algo como FtpWebRequest. (Ou existe uma opção melhor e gratuita que já faz parte do Visual Studio?)
Este método funciona, mas é MUITO lento. (Suponho que devido ao constante logon / 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.
Em vez disso, provavelmente deveríamos estar fazendo isso, mas nunca funciona:
Log in with user name and password. ONCE. Get a list of filenames. Download each file. Log out ONCE.
Encontramos inúmeros exemplos on-line de "como obter uma lista de arquivos FTP" e mais tarde "como baixar um arquivo com FTP" ... mas nunca vemos "obter CADA nome de arquivo e fazer o download AGORA".
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