Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionDim errorResponse As FtpWebResponse = ex.Response
fi = Nothing
Public Function CheckIfFtpFileExists(ByVal fileUri As String) As Boolean
Dim request As FtpWebRequest = WebRequest.Create(fileUri)
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.GetFileSize
Try
Dim response As FtpWebResponse = request.GetResponse()
' THE FILE EXISTS
Catch ex As WebException
Dim response As FtpWebResponse = ex.Response
If FtpStatusCode.ActionNotTakenFileUnavailable = response.StatusCode Then
' THE FILE DOES NOT EXIST
Return False
End If
End Try
Return True
end function
Dim di As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(localpath)
Dim monCheck As String
For Each fi As System.IO.FileInfo In di.GetFiles
monCheck = fi.ToString
if not CheckIfFtpFileExists(baseUri1 & "/" & monCheck) then
messagebox.show("file will be deleted")
end if
Private Shared Function GetDirectoryListing(uri As String, credentials As ICredentials) As IList(Of String)
Dim entries As New List(Of String)()
Dim request = DirectCast(WebRequest.Create(uri), FtpWebRequest)
If credentials IsNot Nothing Then
request.Credentials = credentials
End If
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails
Using response = DirectCast(request.GetResponse(), FtpWebResponse),
stream = response.GetResponseStream(),
reader = New StreamReader(stream)
Dim line As String = reader.ReadLine()
While line IsNot Nothing
entries.Add(line)
line = reader.ReadLine()
End While
Return entries
End Using
End Function
Dim wc As New WebClient()
wc.Credentials = New NetworkCredential(Login, Password)
Dim baseUri1 = "ftp://monftp....."
Dim fileNames1 = GetDirectoryListing(baseUri1, wc.Credentials)
Dim di As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(localpath)
For Each fi As System.IO.FileInfo In di.GetFiles
BackgroundWorker3.ReportProgress(0, fi.ToString & " en test...")
Threading.Thread.Sleep(100)
Dim monCheck As String = fi.ToString
Dim checkOk As Boolean = False
For Each fileName In fileNames1
If (fileName.StartsWith("-")) Then
Dim Monsplit1 = Split(fileName, " ")(UBound(Split(fileName, " ")))
If monCheck = Monsplit1 Then
checkOk = True
End If
End If
Next
If checkOk = True Then
BackgroundWorker3.ReportProgress(0, monCheck & " est OK")
Else
BackgroundWorker3.ReportProgress(0, monCheck & " en trop...")
'MsgBox(localpath & monCheck & vbLf & "Va être supprimé !!!")
My.Computer.FileSystem.DeleteFile(localpath & monCheck)
Threading.Thread.Sleep(100)
End If
Next
End Sub
26 nov. 2014 à 20:10