Most general way to make a Uri object?
Question
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?
Solution
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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow