VB.net - see if remote file exists

2019-07-18 03:13发布

问题:

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

回答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.