89

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...

Howie
  • 2,797
  • 6
  • 27
  • 58

4 Answers4

190
string uriPath =
    "file:\\C:\\Users\\john\\documents\\visual studio 2010\\Projects\\proj";
string localPath = new Uri(uriPath).LocalPath;
Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
  • 3
    Nice! I had this exact same problem and Googled like crazy, this worked great. – Jeremy Morgan Nov 25 '12 at 13:47
  • While its a solution that works ofc, there is but one case which it fails. If the location contains a # then the local path will only go until that position. E.g. if your path is file:///C:/foo/bar#/test.jpg then you will get C:/foo/bar instead of what you actually want. – DokutoMekki Oct 04 '15 at 08:15
  • 2
    Hi. I want the file to be present at www.xxx.com/sitemap.xml. This thing is working at localhost, but on server there is no such directory. How can things be sorted in that case? – It's a trap Dec 08 '16 at 09:41
  • @It'satrap Perhaps you are looking for [Server.MapPath](https://stackoverflow.com/q/275781/1115360). – Andrew Morton Jan 06 '20 at 10:08
  • but after using your path, I am creating more folder inside and its not creating the directory. have a look https://stackoverflow.com/questions/63493899/the-network-name-cannot-be-found-error-while-uploading-images-in-c-sharp-asp-net – hud Aug 19 '20 at 20:50
1

Try This

ImagePath = "http://localhost/profilepics/abc.png";
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ImagePath);
          HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream receiveStream = response.GetResponseStream();
Hardeep Singh
  • 684
  • 8
  • 12
0

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

0
     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 { }
  • 1
    While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Artog Sep 10 '19 at 11:38