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

BCS*_*BCS 15 c# types uri path

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

我应该使用现有的C#类型吗?URI类型可能会奏效,但在路过一目了然,它似乎只是URI.

Eri*_*bal 23

使用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());
Run Code Online (Sandbox Code Playgroud)

给我这个输出:

file:///c:/temp/myfile.bmp  
file:///c:/temp/woohoo/temp.bmp  
http://www.google.com/blahblah.html
Run Code Online (Sandbox Code Playgroud)

  • 好吧,你可以检查"LocalPath"属性,它将以本地格式提供文件名(没有文件:///). (2认同)