BCS BCS - 3 years ago 176
C# Question

c# type to handle relative and absolute URI's and local file paths

I have a use cases where I will be dealing with both local file paths (e.g.

) and URI's (e.g.
). I also will be dealing with both relative and absolute paths so I need functionality like
and friends.

Is there an existing C# type I should use? The Uri type might work but at a passing glance, it seems to be URI only.

Answer Source

Using the Uri class, it seems to be working. It turns any file path to the `file:///..." syntax in the Uri. It handles any URI as expected, and it has capacity to deal with relative URIs. It depends on what else you are trying to do with that path.

(Updated to show the use of relative Uri's):

string fileName = @"c:\temp\myfile.bmp";
string relativeFile = @".\woohoo\temp.bmp";
string addressName = @"http://www.google.com/blahblah.html";

Uri uriFile = new Uri(fileName);
Uri uriRelative = new Uri(uriFile, relativeFile);
Uri uriAddress = new Uri(addressName);


Gives me this output:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download