VB.net - 查看远程文件是否存在 [英] VB.net - see if remote file exists

查看:65
本文介绍了VB.net - 查看远程文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以在传递 URL 后检查远程文件是否存在.假设它不存在,该函数将返回 0 以在另一个子程序中使用.这是我所拥有的:

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

当我运行应用程序并故意提供一个不存在的 URL 时,Visual Studio 给我 System.Net.WebException 未处理.Message=远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问).

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).

我假设if response.StatusCode..."会处理这个问题,而不是关闭程序.

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

感谢任何帮助.

DWM

推荐答案

首先你应该从 Integer 切换到 Boolean 因为你只返回 1 或 0.Boolean 可以是 True 或 False.

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.

其次,您应该将所有内容包装在 Try/Catch 块中以处理可能发生的任何错误.在 Try/Catch 中包装代码可以捕获大多数错误(除了最极端的错误)并将其放在可能引发错误的代码周围,从而避免应用程序崩溃越简单的错误.

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.

最后,您应该使用 Return 而不是 RemoteFileExists = <value>,因为 Return 都将返回想要的值并退出函数.

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

示例实现:

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

Catch 块中,ex.Message 是错误消息,而 ex.StackTrace 是代码中发生错误的位置.

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

这篇关于VB.net - 查看远程文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆