tags:

views:

65

answers:

2

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:\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!

+6  A: 

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
Joey
Hacked this: Debug.Assert(thePath.StartsWith(@"C:\")); string reversed = thePath.Replace(@"\", @"/"); string uriSeed = @"file:///" + reversed; Uri resUri = new Uri(uriSeed);
Avi
+1  A: 

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

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

Catdirt