Question

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?

Was it helpful?

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

OTHER TIPS

can you not just do this:

    If My.Computer.Network.Ping("IP HERE") Then
        ' Success
    Else
        'fail
    End If

Thanks

Paul

Typically you write your code in a way that assumes that everything is perfect, then add error handling for cases where the connection fails.

Verifying whether the server is online beforehand does not guarantee that it will stay that way for the duration of the user session.

- (void)newBuddyOnline:(NSString *)buddyName {

[onlineBuddies addObject:buddyName];
[self.TableView reloadData];

}

- (void)buddyWentOffline:(NSString *)buddyName {

[onlineBuddies removeObje`
`ct:buddyName];
[self.TableView reloadData];
}

This may helps .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top