1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
Public Function GetFTPDirectory(ByVal _FTPUri As String, ByVal _Credential As NetworkCredential) As List(Of String)
Dim result As New List(Of String)
Dim _Request As FtpWebRequest = WebRequest.Create(_FTPUri)
With _Request
.Credentials = _Credential
.Method = WebRequestMethods.Ftp.ListDirectory
End With
Try
Dim _ResponseStream As Stream = _Request.GetRequestStream()
Dim _ReponseStreamReader As New StreamReader(_ResponseStream)
While Not _ReponseStreamReader.EndOfStream
result.Add(_ReponseStreamReader.ReadLine)
End While
Return result
Catch ex As WebException
MessageBox.Show(ex.Message)
Return Nothing
End Try
End Function
Public Function FTPDownLoadXLSFiles(ByVal _FTPUri As String, ByVal _Credential As NetworkCredential, ByVal _LocalDownLoadFolder As String, Optional ByVal _MoveFileAfterDownLoad As Boolean = True) As List(Of String)
Dim _Wc As New WebClient
_Wc.Credentials = _Credential
Dim _DownloadedFiles As New List(Of String) 'permettra de stocker les noms de fichiers téléchargés
'On filtre la liste des fichiers pour ne récupérer que les extensions .xls
Dim result As List(Of String) = From _File As String In GetFTPDirectory(_FTPUri, _Credential) Select _File Where _File.EndsWith(".xls", StringComparison.CurrentCultureIgnoreCase)
'Pour chaque fichier, vérifie que le fichier n'existe pas dans le dossier de destination
For Each _File As String In result.ToList
Try
Dim _LocalFileURI As String = Path.Combine(_LocalDownLoadFolder, _File)
Dim _FTPFileUri As String = Path.Combine(_FTPUri, _File)
If File.Exists(_LocalFileURI) Then
If MessageBox.Show("Le fichier " & _File & " est déjà présent dans le dossier " & _LocalDownLoadFolder _
& ". Voulez vous le remplacer?", "Le fichier existe déjà", MessageBoxButtons.YesNo, _
MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then
'si l'utilisateur clique sur OUI, on efface le fichier
File.Delete(_LocalFileURI)
Else
'Si non, on le déplace vers un dossier d'archive
File.Move(_LocalFileURI, My.Settings.DownloadArchive)
End If
End If
_Wc.DownloadFile(_FTPFileUri, _LocalFileURI)
_DownloadedFiles.Add(_LocalFileURI)
If _MoveFileAfterDownLoad Then
'On exécute la procédure de déplacement
End If
Catch ex As FileNotFoundException
MessageBox.Show(ex.Message)
Catch ex As WebException
MessageBox.Show(ex.Message)
Exit Try
End Try
Next
Return _DownloadedFiles
End Function |
Partager