Salve ragazzi,
sto facendo un programmino che dovrebbe scaricare tramite ftp un'intera cartella.
Io ho fatto cosi:
codice:
Private Sub Download_File(Destination As String, NomeCartella As String, fileName As String)
Dim reqFTP As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Dim outputStream As FileStream = Nothing
Try
If Directory.Exists(Destination & NomeCartella) = False Then
Directory.CreateDirectory(Destination & NomeCartella)
End If
outputStream = New FileStream(Destination & fileName, FileMode.Create)
reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + gTpSettaggi.IpServer & "/" & fileName)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(gTpSettaggi.Utente, gTpSettaggi.Pwd)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
Dim cl As Long = response.ContentLength
Dim bufferSize As Integer = 2048
Dim readCount As Integer
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
readCount = ftpStream.Read(buffer, 0, bufferSize)
While readCount > 0
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
End While
ftpStream.Close()
outputStream.Close()
response.Close()
Catch ex As Exception
If outputStream IsNot Nothing Then
outputStream.Close()
End If
If ftpStream IsNot Nothing Then
ftpStream.Close()
ftpStream.Dispose()
End If
CreaLogError("Impossibile scaricare il file: " & fileName & ". " & ex.Message)
End Try
End Sub
In pratica richiamo la precedente Sub cosi:
codice:
For i As Integer = 0 To appList.Count - 1
Download_File(Destinazione, PathCartella, gTpSettaggi.PathCartella & "/" & appList(i))
Next i
Dove appList è un arraylist contenente i nomi di tutti i file da scaricare.
La procedura funziona correttamente, ma ho notato che ogni tanto, va in errore il download di qualche file. Ora la mia domanda era: Esiste un'alternativa alla mia procedura, che mi consenta di scaricare il contenuto di un'intera cartella?.
Se avete notato, il codice da me scritto, apre e chiude la connessione per ogni file da scaricare. E' questo il problema?