2

I have a function to check if a remote file exists after being passed the URL. Assuming it doesn't exist the function would return 0 to be used in another sub. Here's what I have:

Public Function RemoteFileExists(ByVal fileurl As String) As Integer
    Dim request As FtpWebRequest = DirectCast(WebRequest.Create(fileurl), FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.GetFileSize
    Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
    If response.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
        RemoteFileExists = 0
        Exit Function
    End If
    Dim fileSize As Long = response.ContentLength
    MsgBox(fileSize)
    If fileSize > 0 Then
        RemoteFileExists = 1
    Else
        RemoteFileExists = 0
    End If
End Function

When I run the app and purposely supply a URL that doesn't exist Visual Studio gives me System.Net.WebException was unhandled. Message=The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

I assumed that the "if response.StatusCode..." would handle that rather than shutting down the program.

Any help appreciated.

DWM

David Mize
  • 23
  • 3
  • How about wrapping everything in a `Try`/`Catch` block, and return false when an exception is thrown? – Visual Vincent Mar 26 '16 at 19:09
  • By the way, **1)** Why are you using an `Integer` as return type instead of `Boolean`? **2)** Call `Return 0` or `Return 1` instead of `RemoteFileExists = ...` and `Exit Function` (calling `Return` also exits the function). – Visual Vincent Mar 26 '16 at 19:11
  • I was using an integer because that's what I could figure out. I tried the try/catch but couldn't get it right. I'm fairly new to VB. If you'd care to offer an example I'd be deeply appreciative. – David Mize Mar 27 '16 at 03:52
  • An example is given! – Visual Vincent Mar 27 '16 at 05:55

1 Answers1

1

First of all you should switch from Integer to Boolean since you only return either 1 or 0 anyway. A Boolean can be either True or False.

Secondly, you should wrap everything in a Try/Catch block to handle any error that might occur. Wrapping code in Try/Catch can catch most errors (except for the most extreme ones) and putting it around code that could throw an error saves you from having your application crash for the more simple errors.

And finally, you should use Return <value> instead of RemoteFileExists = <value>, since Return will both return the wanted value AND exit the function.

Example implementation:

Public Function RemoteFileExists(ByVal fileurl As String) As Boolean
    Try
        Dim request As FtpWebRequest = DirectCast(WebRequest.Create(fileurl), FtpWebRequest)
        request.Method = WebRequestMethods.Ftp.GetFileSize
        Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
        If response.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
            Return False 'Return instead of Exit Function
        End If
        Dim fileSize As Long = response.ContentLength
        MsgBox(fileSize)
        If fileSize > 0 Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception 'Catch all errors
        'Log the error if you'd like, you can find the error message and location in "ex.Message" and "ex.StackTrace".
        MessageBox.Show("An error occurred:" & Environment.NewLine & ex.Message & Environment.NewLine & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Return False 'Return False since the checking failed.
    End Try
End Function

In the Catch block, ex.Message is the error message, and ex.StackTrace is where in the code the error occurred.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Thank you. I'm learning VB by looking at examples of code and this is helpful. It's also helpful that you actually answered my question and did so without any of the condescension I find often on forums. God bless. DWM – David Mize Mar 27 '16 at 21:08
  • @DavidMize : Glad it worked and satisfied you! I'd be glad if you would mark my post as the accepted answer by pressing the green check mark on it's left. This will reward the answerer (me) with 15 reputation points, and it will give you (as the asker) 2 rep. points. – Visual Vincent Mar 27 '16 at 21:16