1

检查文件是否在线存在的最快和最简单的方法是什么?

我知道如何检查文件系统上是否存在文件或网站是否存在,但我不知道如何检查文件是否在线存在。我该怎么做呢?

我想验证以下链接是否在线存在:

http://ccc-itgs2012.wikispaces.com/file/view/AP+Human+Geography+Gapminder.flv

4

4 回答 4

1

对于 HTTP,使用 HTTP HEAD 方法。它的行为类似于 GET 方法,只是它只返回内容标头。如果文件不存在,服务器应该返回 404 状态码。否则,您可以假设该文件存在(甚至从内容标题中获取其大小)。

编辑

您可以使用以下代码:

Public Function ResourceExists(location As Uri) As Boolean
    If (Not String.Equals(location.Scheme, Uri.UriSchemeHttp, StringComparison.InvariantCultureIgnoreCase)) And (Not String.Equals(location.Scheme, Uri.UriSchemeHttps, StringComparison.InvariantCultureIgnoreCase)) Then
        Throw New NotSupportedException("URI scheme is not supported")
    End If

    Dim request = Net.WebRequest.Create(location)
    request.Method = "HEAD"

    Try
        Using response = request.GetResponse
            Return DirectCast(response, Net.HttpWebResponse).StatusCode = Net.HttpStatusCode.OK
        End Using
    Catch ex As Net.WebException
        Select Case DirectCast(ex.Response, Net.HttpWebResponse).StatusCode
            Case Net.HttpStatusCode.NotFound
                Return False
            Case Else
                Throw
        End Select
    End Try
End Function

用法:

Dim itExists As Boolean = ResourceExists(New Uri("http://ccc-itgs2012.wikispaces.com/file/view/AP+Human+Geography+Gapminder.flv"))

这确实会锁定调用者的线程,因此您可能希望将其重构为异步方法。

于 2013-01-25T15:08:25.983 回答
1

您以与检查文件系统相同的方式在线检查:尝试访问文件,并在尝试失败时处理异常(顺便说一句:如果您在文件系统上使用 File.Exists(),您可能正在做它错了)。

唯一的额外问题是,在线检查时,您可以发送资源的 http 请求并访问响应流,而无需实际阅读流,这意味着您不必下载整个文件即可知道请求将完成。

于 2013-01-25T14:53:19.780 回答
0

此代码为我工作

Public Function CheckAddress(ByVal URL As String) As Boolean
    Try
        Dim request As WebRequest = WebRequest.Create(URL)
        Dim response As WebResponse = request.GetResponse()
    Catch ex As Exception
        Return False
    End Try
    Return True
End Function
于 2015-12-21T16:27:12.810 回答
0

我想出的最好的方法实际上发现它可以按需要工作,因为所有的方法都只是检查是否找到了域,所以如果找到了域而文件没有找到,它仍然会告诉你找到该文件,我的方法是尝试下载该文件DownloadFileAsync并使用计时器运行 3 秒,然后在 3 秒后检查是否从该文件下载了任何字节,如果是,则通过该文件找到该文件链接,如果没有,则找不到,然后您可以取消下载并删除已在您的PC上创建的文件。

这是我的代码:

Imports System.IO
Imports System.Net
Public Class Form1
    Dim BytesReceived As Integer

    Public WithEvents Download As WebClient

    Private Sub DownloadBUT_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Download = New WebClient
        Download.DownloadFileAsync(New Uri("http://example.com/FileName.exe"), My.Computer.FileSystem.SpecialDirectories.Desktop & "FileName.exe")
        CheckFileTimer.Start()
        DownloadBUT.Enabled = False
    End Sub

    Private Sub Download_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles Download.DownloadProgressChanged
        BytesReceived = e.BytesReceived
    End Sub

    Private Sub CheckFileTimer_Tick(sender As Object, e As EventArgs) Handles CheckFileTimer.Tick
        CheckFileTimer.Stop()
        If BytesReceived = 0 Then
            MsgBox("File not found!")
        Else
            MsgBox("File found!")
        End If
        DownloadBUT.Enabled = True
        Download.CancelAsync()
        Try
            File.Delete(My.Computer.FileSystem.SpecialDirectories.Desktop & "FileName.exe")
        Catch ex As Exception : End Try
    End Sub
End Class

希望这对你有用:)

于 2017-07-10T19:12:26.643 回答