c# 类型来处理相对和绝对 URI 以及本地文件路径 [英] c# type to handle relative and absolute URI's and local file paths
问题描述
我有一个用例,我将同时处理本地文件路径(例如 c:fooar.txt
)和 URI(例如 http://somehost.com/fiz/baz
).我还将处理相对路径和绝对路径,因此我需要像 Path.Combine
和朋友们.
I have a use cases where I will be dealing with both local file paths (e.g. c:fooar.txt
) and URI's (e.g. http://somehost.com/fiz/baz
). I also will be dealing with both relative and absolute paths so I need functionality like Path.Combine
and friends.
我应该使用现有的 C# 类型吗? Uri 类型 可能有用,但乍一看,它似乎只是 URI.
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.
推荐答案
使用 Uri 类,它似乎有效.它将任何文件路径转换为 Uri 中的 `file:///..." 语法.它按预期处理任何 URI,并且它具有处理相对 URI 的能力.这取决于您还想做什么那条路.
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.
(更新以显示相对 Uri 的使用):
(Updated to show the use of relative Uri's):
string fileName = @"c: empmyfile.bmp";
string relativeFile = @".woohoo emp.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
这篇关于c# 类型来处理相对和绝对 URI 以及本地文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!