C#类型来处理相对和绝对URI的和本地文件路径 [英] c# type to handle relative and absolute URI's and local file paths

查看:1260
本文介绍了C#类型来处理相对和绝对URI的和本地文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我会处理本地文件路径的使用情况下(如 C:\\富\\跳回到bar.txt )和URI的(如 http://somehost.com/fiz/baz )。我也将与相对和绝对路径来处理,所以我需要一个像<一个功能href=\"http://msdn.microsoft.com/en-us/library/system.io.path.combine%28VS.71%29.aspx\"><$c$c>Path.Combine和朋友。

I have a use cases where I will be dealing with both local file paths (e.g. c:\foo\bar.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类,它似乎是工作。它可以将任何文件路径`的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:\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());

给我这样的输出:

Gives me this output:

文件:/// C:/temp/myfile.bmp

  文件:/// C:/temp/woohoo/temp.bmp

   http://www.google.com/blahblah.html

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

这篇关于C#类型来处理相对和绝对URI的和本地文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆