Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 638333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:43:17+00:00 2026-05-13T20:43:17+00:00

I wonder if someone can help me; In PHP you can use fopen(Path) to

  • 0

I wonder if someone can help me;

In PHP you can use fopen(Path) to read from a file. The Path can be either a local file (in the usual formats /etc/file or c:\file.txt) OR it can be a URL in which case PHP will open a http connection and read from the remote location

I’d like to achieve the same in VB.Net. I’m not aware of any framework function which will achieve this.

I’m currently pattern matching the path against a Regex for a valid URL – If I get a match, I open the file using a httpwebrequest otherwise I try to use the local file system.

This is all a little bit hacky – I’m hoping to find a better way to implement this.

Any advice or suggestions would be appreciated.

        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: I’m aware that the logic above is inefficient and actually ends up calling IsURL() more than it has to – I’m intending to tidy it up if I can’t find a more elegant solution.

Thanks in advance for any help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:43:17+00:00Added an answer on May 13, 2026 at 8:43 pm

    Another Option:
    It looks like the base class webrequest will work for local and http files. It was hiding under our nose the whole time. Let me know if this does not meet your needs in any way.

        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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder whether someone can help me please. I have the following xml file
I wonder is someone can help, I'm building a website, which is driven from
wonder whether someone can help me with the following one... I have a struct
I wonder if someone can help me. I'm trying to add a reference to
I wonder if someone can help me. I'm trying to display a toast element
I wonder if someone can help: Long story short, I'm using MSSQL2005 to build
I wonder if someone can help shed some light on this: I drop a
I wonder whether someone can help me please. I'm using the following script to
I wonder if someone can help me out with regards to the memory management
I wonder whether someone can help me please. I've created this form which allows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.