Exception: "URI formats are not supported"

C#.Net.Net 4.0

C# Problem Overview


I have an absolute local path pointing to a dir: "file:\\C:\\Users\\john\\documents\\visual studio 2010\\Projects\\proj"

But when I try to throw it into DirectoryInfo's ctor I get the "URI formats are not supported" exception.

I googled and looked on SO, but I only see solutions with remote paths, not local ones. I'd expect a conversion method of some sort...

C# Solutions


Solution 1 - C#

string uriPath =
    "file:\\C:\\Users\\john\\documents\\visual studio 2010\\Projects\\proj";
string localPath = new Uri(uriPath).LocalPath;

Solution 2 - C#

Try This

ImagePath = "http://localhost/profilepics/abc.png";
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ImagePath);
          HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream receiveStream = response.GetResponseStream();

         

Solution 3 - C#

I solved the same error with the Path.Combine(MapPath()) to get the physical file path instead of the http:/// www one.

Solution 4 - C#

     string ImagePath = "";
 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ImagePath);
        string a = "";
        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
            Stream receiveStream = response.GetResponseStream();
            if (receiveStream.CanRead)
            { a = "OK"; }
        }
       
        catch { }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHowieView Question on Stackoverflow
Solution 1 - C#Marc GravellView Answer on Stackoverflow
Solution 2 - C#Hardeep SinghView Answer on Stackoverflow
Solution 3 - C#Robert Peter BronsteinView Answer on Stackoverflow
Solution 4 - C#Badr BouaichiView Answer on Stackoverflow