Utilizando FTP para descargar cada archivo * MIENTRAS * obteniendo la lista de archivos
Necesitamos obtener alrededor de 100 archivos muy pequeños de un servidor FTP remoto usando vb.net. Nuestra empresa no nos permite comprar (o instalar) ninguna biblioteca de ftp de terceros ... por lo que nos vemos obligados a usar algo como FtpWebRequest. (¿O hay una mejor opción gratuita que ya forma parte de Visual Studio?)
Este método funciona, pero es MUY lento. (Supongo que debido al inicio / cierre de sesión constante).
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.
En cambio, probablemente deberíamos estar haciendo esto, pero nunca funciona:
Log in with user name and password. ONCE. Get a list of filenames. Download each file. Log out ONCE.
Encontramos innumerables ejemplos en línea de "obtener una lista de archivos FTP" y más adelante "cómo descargar 1 archivo con FTP" ... pero nunca vemos "obtener CADA nombre de archivo y descargarlo AHORA".
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