VB.net - 查看远程文件是否存在
VB.net - see if remote file exists
我有一个函数可以在通过 URL 后检查远程文件是否存在。假设它不存在,该函数将 return 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=远程服务器 return 出现错误:(550) 文件不可用(例如,未找到文件,无法访问)。
我假设 "if response.StatusCode..." 会处理这个问题而不是关闭程序。
感谢任何帮助。
DWM
首先,您应该从 Integer
切换到 Boolean
,因为无论如何您只能 return 1 或 0。 Boolean
可以是 True 或 False。
其次,您应该将所有内容包装在 Try
/Catch
块中以处理可能发生的任何错误。将代码包装在 Try
/Catch
中可以捕捉到大多数错误(除了最极端的错误)并将它放在可能引发错误的代码周围可以避免应用程序因更简单的错误而崩溃。
最后,您应该使用 Return <value>
而不是 RemoteFileExists = <value>
,因为 Return
会 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
是代码中发生错误的地方。
我有一个函数可以在通过 URL 后检查远程文件是否存在。假设它不存在,该函数将 return 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=远程服务器 return 出现错误:(550) 文件不可用(例如,未找到文件,无法访问)。
我假设 "if response.StatusCode..." 会处理这个问题而不是关闭程序。
感谢任何帮助。
DWM
首先,您应该从 Integer
切换到 Boolean
,因为无论如何您只能 return 1 或 0。 Boolean
可以是 True 或 False。
其次,您应该将所有内容包装在 Try
/Catch
块中以处理可能发生的任何错误。将代码包装在 Try
/Catch
中可以捕捉到大多数错误(除了最极端的错误)并将它放在可能引发错误的代码周围可以避免应用程序因更简单的错误而崩溃。
最后,您应该使用 Return <value>
而不是 RemoteFileExists = <value>
,因为 Return
会 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
是代码中发生错误的地方。