开发者

Why does IsWellFormedOriginalString fail on file Uris?

I have code like this:

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

But goodCond is false! What am I doing wrong?


Edit: Thanks Johannes and Catdirt. I'll focus my question: How do I convert a valid file path to a valid file Uri (using uri.IsWellFormedOriginalString as an indication to the validity of the Uri)? Take a look at this:

        DirectoryInfo di = new DirectoryInfo(@"c开发者_JS百科:\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 is a well formed path, but still goodCond is bad!


Your URI is not well-formed.

A well-formed example would be 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


It's false because it's not well formed.

http://msdn.microsoft.com/en-us/library/system.uri.iswellformedoriginalstring.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜