Pregunta

Necesitamos obtener alrededor de 100 archivos muy pequeños de un servidor FTP remoto usando VB.NET. Nuestra empresa no nos permitirá comprar (ni instalar) ninguna biblioteca FTP de terceros ... por lo que nos vemos obligados a usar algo como FTPWebRequest. (¿O hay una mejor opción gratuita que ya es parte de Visual Studio?)

Este método funciona, pero es muy lento. (Supongo que debido a la constante registro en/apagado).

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 luego "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

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top