Question

Nous devons obtenir environ 100 très petits fichiers à partir d'un serveur FTP distant à l'aide de VB.NET. Notre entreprise ne nous laissera pas acheter (ou installer) des bibliothèques FTP tierces ... nous sommes donc obligés d'utiliser quelque chose comme FTPWeBRequest. (Ou y a-t-il un meilleur choix gratuit qui fait déjà partie de Visual Studio?)

Cette méthode fonctionne, mais elle est très lente. (Je suppose en raison de la journalisation constante dans / 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.

Au lieu de cela, nous devrions probablement le faire, mais cela ne fonctionne jamais:

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

Nous avons trouvé d'innombrables exemples en ligne de "Obtenir une liste de fichiers FTP" et plus tard "Comment télécharger 1 fichier avec FTP" ... mais nous ne voyons jamais "Obtenez chaque nom de fichier et téléchargez-le maintenant".

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

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top