例外:“不支持URI格式”

时间:2012-09-24 07:35:28

标签: c# .net .net-4.0

我有一个指向dir的绝对本地路径:"file:\\C:\\Users\\john\\documents\\visual studio 2010\\Projects\\proj"

但是当我尝试将它扔进DirectoryInfo的ctor时,我得到了“不支持URI格式”的例外。

我用Google搜索并查看了SO,但我只看到了远程路径的解决方案,而不是本地解决方案。我期待某种转换方法......

4 个答案:

答案 0 :(得分:170)

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

答案 1 :(得分:1)

试试这个

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

答案 2 :(得分:0)

我用Path.Combine(MapPath())解决了相同的错误,以获取物理文件路径而不是http:/// www。

答案 3 :(得分: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 { }