Check if FTP is online/offline
ftp-server, ping, vb.net, visual-studio
Solution
Using webrequest, try this code i found
Hi, this should work fine:
Imports System.Net
Dim request = _
DirectCast(WebRequest.Create _
("ftp://ftp.example.com/folder_here/"), FtpWebRequest)
request.Credentials = _
New NetworkCredential("user_here", "pass_here")
request.Method = WebRequestMethods.Ftp.ListDirectory
Try
Using response As FtpWebResponse = _
DirectCast(request.GetResponse(), FtpWebResponse)
' Folder exists here
MsgBox("exists!")
End Using
Catch ex As WebException
Dim response As FtpWebResponse = _
DirectCast(ex.Response, FtpWebResponse)
'Does not exist
If response.StatusCode = _
FtpStatusCode.ActionNotTakenFileUnavailable Then
MsgBox("Doesn't exist!")
End If
End Try
..the idea is that we use FtpWebRequest class and pass the folder name with a trailing slash "/", if the folder is found then the response will be processed fine inside Try-Catch block, if the folder could not be found, we handle the exception controlling with statusCode (ActionNotTakenFileUnavailable) to determine if absence of folder causes exception. That should work fine.
Sources First answer
--------------- Please also Try ----------------------
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
Get’s called like this:
If CheckIfFtpFileExists("ftp://ftp.domain.com/filename.txt") Then
' Do something
End If
Sources
Problem
What is the best way to determine if a ftp server is online or offline in Visual Basic? I have tried many different ways of pinging the ftp server, but whatever I try, I get this error: ``` An exception occurred during a Ping request. ``` Is this easily fixable? Or is there a better method than what I'm using?