Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.Net-打开本地或远程文件(通过http)_.net_Vb.net_Fopen - Fatal编程技术网

VB.Net-打开本地或远程文件(通过http)

VB.Net-打开本地或远程文件(通过http),.net,vb.net,fopen,.net,Vb.net,Fopen,我想知道是否有人能帮助我 在PHP中,您可以使用fopen(Path)读取文件。路径可以是本地文件(通常格式为/etc/file或c:\file.txt),也可以是URL,在这种情况下,PHP将打开http连接并从远程位置读取 我想在VB.Net中实现同样的功能。我不知道有什么框架功能可以实现这一点 我目前正在将路径与有效URL的正则表达式进行模式匹配-如果匹配,我将使用httpwebrequest打开文件,否则我将尝试使用本地文件系统 这是一个有点黑客-我希望找到一个更好的方法来实现这一点 如

我想知道是否有人能帮助我

在PHP中,您可以使用fopen(Path)读取文件。路径可以是本地文件(通常格式为/etc/file或c:\file.txt),也可以是URL,在这种情况下,PHP将打开http连接并从远程位置读取

我想在VB.Net中实现同样的功能。我不知道有什么框架功能可以实现这一点

我目前正在将路径与有效URL的正则表达式进行模式匹配-如果匹配,我将使用httpwebrequest打开文件,否则我将尝试使用本地文件系统

这是一个有点黑客-我希望找到一个更好的方法来实现这一点

如有任何意见或建议,将不胜感激

        Private Function RetrieveBGImage() As Image
        Dim Ret As Image
        If Not (IsURL(_BackgroundImage) Or IsLocalFile(_BackgroundImage)) Then
            Throw New Exception(String.Format("Unable to load from uri ""{0}""", _BackgroundImage))
        End If
        If IsURL(_BackgroundImage) Then
            Dim Response As Net.HttpWebResponse = Nothing
            Try
                Dim Request As Net.HttpWebRequest = DirectCast(Net.HttpWebRequest.Create(_BackgroundImage), Net.HttpWebRequest)
                Response = DirectCast(Request.GetResponse, Net.HttpWebResponse)
                Ret = Image.FromStream(Response.GetResponseStream())
            Catch ex As Exception
                Throw New Exception(String.Format("Unable to load uri: ""{0}"" using HTTPWebRequest. {1}", _BackgroundImage, ex.Message), ex)
            Finally
                If Response IsNot Nothing Then
                    Response.Close()
                End If
                Response = Nothing
            End Try
        Else
            Try
                Ret = Image.FromFile(_BackgroundImage)
            Catch ex As Exception
                Throw New Exception(String.Format("Unable to load uri ""{0}"" using local file system. {1}", _BackgroundImage, ex.Message), ex)
            End Try
        End If
        Return Ret
    End Function

    Private Function IsURL(ByVal Path As String) As Boolean
        Dim RegexObj As New Regex("^(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w\d{1-3}]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|edu|co\.uk|ac\.uk|it|fr|tv|museum|asia|local|travel|[a-z]{2})?)(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?$")
        Dim MatchObj As Match = RegexObj.Match(Path)
        Return MatchObj.Success
    End Function

    Private Function IsLocalFile(ByVal Path As String) As Boolean
        Dim Ret As Boolean = False
        Try
            Dim FInfo As New FileInfo(_BackgroundImage)
            Ret = FInfo.Exists

        Catch handledex As ArgumentException
            'Ignore invalid path errors
        Catch ex As Exception
            Throw New Exception(String.Format("Unable to load uri ""{0}"" using local file system. {1}", Path, ex.Message), ex)
        End Try
        Return Ret
    End Function
NB:我知道上面的逻辑效率很低,实际上调用IsURL()的次数超过了它必须调用的次数——如果找不到更优雅的解决方案,我打算整理一下

提前感谢您的帮助

我相信您可以使用它来完成这两项任务。它不会生成流,但如果您有一个临时目录来放置文件,那么DownloadFile可以完成所有繁重的列表,您可以从已知位置打开临时文件。我运行了这个快速测试,以确保它在URL和文件中正常工作

    My.Computer.Network.DownloadFile("c:/test1.txt", "C:/test2.txt")
    My.Computer.Network.DownloadFile("http://google.com", "C:/test3.txt")
如果这在任何方面都不能满足您的需要,请告诉我。

我相信您可以同时使用它。它不会生成流,但如果您有一个临时目录来放置文件,那么DownloadFile可以完成所有繁重的列表,您可以从已知位置打开临时文件。我运行了这个快速测试,以确保它在URL和文件中正常工作

    My.Computer.Network.DownloadFile("c:/test1.txt", "C:/test2.txt")
    My.Computer.Network.DownloadFile("http://google.com", "C:/test3.txt")
    Private Function RetrieveBGImage() As Image
    Dim Ret As Image

    Dim Response As Net.WebResponse = Nothing
    Try
        Dim Request As Net.WebRequest = Net.WebRequest.Create(_BackgroundImage)
        Response = Request.GetResponse
        Ret = Image.FromStream(Response.GetResponseStream())
    Catch ex As Exception
        Throw New Exception(String.Format("Unable to load file", _BackgroundImage, ex.Message), ex)
    Finally
        If Response IsNot Nothing Then
            Response.Close()
        End If
        Response = Nothing
    End Try

    Return Ret
End Function
如果这不能满足您的任何需求,请告诉我。

另一个选项:
    Private Function RetrieveBGImage() As Image
    Dim Ret As Image

    Dim Response As Net.WebResponse = Nothing
    Try
        Dim Request As Net.WebRequest = Net.WebRequest.Create(_BackgroundImage)
        Response = Request.GetResponse
        Ret = Image.FromStream(Response.GetResponseStream())
    Catch ex As Exception
        Throw New Exception(String.Format("Unable to load file", _BackgroundImage, ex.Message), ex)
    Finally
        If Response IsNot Nothing Then
            Response.Close()
        End If
        Response = Nothing
    End Try

    Return Ret
End Function
看起来基类webrequest适用于本地和http文件。它一直藏在我们的鼻子底下。如果这不能满足您的任何需求,请告诉我

    Private Function RetrieveBGImage() As Image
    Dim Ret As Image

    Dim Response As Net.WebResponse = Nothing
    Try
        Dim Request As Net.WebRequest = Net.WebRequest.Create(_BackgroundImage)
        Response = Request.GetResponse
        Ret = Image.FromStream(Response.GetResponseStream())
    Catch ex As Exception
        Throw New Exception(String.Format("Unable to load file", _BackgroundImage, ex.Message), ex)
    Finally
        If Response IsNot Nothing Then
            Response.Close()
        End If
        Response = Nothing
    End Try

    Return Ret
End Function
另一种选择: 看起来基类webrequest适用于本地和http文件。它一直藏在我们的鼻子底下。如果这不能满足您的任何需求,请告诉我

    Private Function RetrieveBGImage() As Image
    Dim Ret As Image

    Dim Response As Net.WebResponse = Nothing
    Try
        Dim Request As Net.WebRequest = Net.WebRequest.Create(_BackgroundImage)
        Response = Request.GetResponse
        Ret = Image.FromStream(Response.GetResponseStream())
    Catch ex As Exception
        Throw New Exception(String.Format("Unable to load file", _BackgroundImage, ex.Message), ex)
    Finally
        If Response IsNot Nothing Then
            Response.Close()
        End If
        Response = Nothing
    End Try

    Return Ret
End Function

它非常棒,比我的烂摊子简单得多。唯一的一个小问题是,我更喜欢在不写入磁盘的情况下执行此操作-我希望处理大量图像,尽管我可以手动清理临时文件,但我不需要它们,因为这是一个缓存,它增加了对定时清理任务或类似任务的要求。。。你知道我有什么办法可以在内存中实现吗?我想你会这么说的。我会四处看看。最坏的情况是:看看反射器中
My.Computer.Network.DownloadFile
的代码,看看它内部的功能。好主意。你评论的时候,我正在看。它调用了基本webrequest,这是我得到下一个答案的想法的地方。它非常好,比我的混乱简单得多。唯一的一个小问题是,我更喜欢在不写入磁盘的情况下执行此操作-我希望处理大量图像,尽管我可以手动清理临时文件,但我不需要它们,因为这是一个缓存,它增加了对定时清理任务或类似任务的要求。。。你知道我有什么办法可以在内存中实现吗?我想你会这么说的。我会四处看看。最坏的情况是:看看反射器中
My.Computer.Network.DownloadFile
的代码,看看它内部的功能。好主意。你评论的时候,我正在看。它调用基本webrequest,这就是我得到下一个答案的想法的地方。