tags:

views:

67

answers:

1

I need to convert all of the following forms into .NET Uri object:

  • "hello.world"
  • "..\something\somthing"
  • "../something/somthing/else"
  • "c:\autoexec.bat"
  • "http://whatever.com/nevermind.html"
  • more or less anything else that you might expect to find in something building local or web path.

I have already checked an new Uri("..\somthing\something") and new Uri("../something/somthing/else") don't work. Also none Uri's statics seem to be for this.

Any ideas beside a big logic tree and/or regex?

+3  A: 

Try the following

new Uri("../whatever",UriKind.RelativeOrAbsolute);

Without the RelativeOrAbsolute flag the Uri will attempt to parse a complete absolute URI. Adding the flag will let it parse essentially any valid URI (relative or absolute)

JaredPar
Now why isn't that the default?! :b
BCS
@BCS, don't know. My guess is it's following a principal of make it strict by default.
JaredPar