为什么文件Uris上的IsWellFormedOriginalString失败?

时间:2010-03-17 11:18:14

标签: c# .net uri

我有这样的代码:

string uriString = @"C:\Temp\test.html";
Uri uri = new Uri(uriString);
bool goodCond = uri.IsWellFormedOriginalString();

但是goodCond是假的!我做错了什么?


编辑: 谢谢Johannes和Catdirt。我将集中讨论我的问题:如何将有效文件路径转换为有效文件Uri (使用uri.IsWellFormedOriginalString作为Uri有效性的指示)? 看看这个:

        DirectoryInfo di = new DirectoryInfo(@"c:\temp");
        FileInfo [] fis = di.GetFiles("test.html");
        FileInfo fi = fis[0];
        string uriString = fi.FullName;
        Uri uri = new Uri(uriString);
        bool goodCond = uri.IsWellFormedOriginalString()

Obviosly fi.fullName是一个结构良好的路径,但仍然是goodCond很糟糕!

2 个答案:

答案 0 :(得分:9)

您的URI格式不正确。

一个结构良好的例子是file:///C:/Temp/test.html

PS Home:> (new-object Uri 'file:///C:/Temp/test.html').IsWellFormedOriginalString()
True
PS Home:> (new-object Uri 'file:///C:\Temp\test.html').IsWellFormedOriginalString()
False
PS Home:> (new-object Uri 'C:\Temp\test.html').IsWellFormedOriginalString()
False
PS Home:> (new-object Uri 'C:/Temp/test.html').IsWellFormedOriginalString()
False

答案 1 :(得分:1)