c#type用于处理相对和绝对URI和本地文件路径

我有一个用例,我将处理本地文件路径(例如c:\foo\bar.txt )和URI(例如http://somehost.com/fiz/baz )。 我也将处理相对路径和绝对路径,所以我需要像Path.Combine和朋友这样的function。

我应该使用现有的C#类型吗? Uri类型可能会起作用但是一目了然,它似乎只是URI。

使用Uri类,它似乎正在工作。 它将任何文件路径转换为Uri中的`file:/// …’语法。它按预期处理任何URI,并且它具有处理相对URI的能力。这取决于您尝试使用的其他内容那条路。

(更新以显示相对Uri的使用):

 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); Console.WriteLine(uriFile.ToString()); Console.WriteLine(uriRelative.ToString()); Console.WriteLine(uriAddress.ToString()); 

给我这个输出:

 file:///c:/temp/myfile.bmp file:///c:/temp/woohoo/temp.bmp http://www.google.com/blahblah.html