Domanda

Dobbiamo ottenere circa 100 file molto piccoli da un server FTP remoto utilizzando VB.NET. La nostra azienda non ci consentirà di acquistare (o installare) librerie FTP di terze parti ... quindi siamo costretti a usare qualcosa come FTPWebRequest. (O esiste una scelta gratuita migliore che fa già parte di Visual Studio?)

Questo metodo funziona, ma è molto lento. (Presumo a causa della costante accesso/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.

Invece, probabilmente dovremmo farlo, ma non funziona mai:

Log in with user name and password.  ONCE.
Get a list of filenames.
Download each file.
Log out ONCE.

Abbiamo trovato innumerevoli esempi online di "Ottenere un elenco di file FTP" e successivamente "Come scaricare 1 file con FTP" ... ma non vediamo mai "Ottieni ogni nome di file e scaricalo ora".

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

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top