【问题标题】:VB.net - see if remote file existsVB.net - 查看远程文件是否存在
【发布时间】:2016-03-26 19:03:47
【问题描述】:

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

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) 文件不可用(例如,找不到文件,无法访问)。

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

任何帮助表示赞赏。

DWM

【问题讨论】:

  • 如何将所有内容包装在Try/Catch 块中,并在抛出异常时返回false?
  • 顺便说一句,1) 为什么你使用Integer 作为返回类型而不是Boolean2) 调用Return 0Return 1 而不是RemoteFileExists = ...Exit Function(调用Return 也会退出函数)。
  • 我使用的是整数,因为这是我能算出来的。我尝试了 try/catch 但无法正确处理。我对VB相当陌生。如果您愿意提供一个例子,我将不胜感激。
  • 举个例子!

标签: vb.net ftpwebrequest


【解决方案1】:

首先,您应该从Integer 切换到Boolean,因为无论如何您只返回1 或0。 Boolean 可以是 True 或 False。

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

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

示例实现:

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 是代码中发生错误的位置。

【讨论】:

  • 谢谢。我通过查看代码示例来学习 VB,这很有帮助。您实际上回答了我的问题并且没有我在论坛上经常发现的任何屈尊俯就,这也很有帮助。上帝保佑。 DWM
  • @DavidMize :很高兴它有效并让您满意!如果您按左侧的绿色复选标记将我的帖子标记为已接受的答案,我会很高兴。这将奖励回答者(我)15 个声望点,它会给你(作为提问者)2 个代表。点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 2011-07-24
  • 2010-11-15
相关资源
最近更新 更多