C# Path.Combine 缺陷(http路径用Uri类)
Path.Combine:

什么时候会用到Path.Combine呢?,当然是连接路径字符串的时候!
所以下面的代码可以完美的工作:
public static void Main()
{
string[] arr_pa = { @"c:\abc\", @"c:\abc" };
string[] arr_pb = { @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, pb));
}
}
}
结果如下:

当然你也可以:
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, "def", pb));
结果是:

从这个例子可以知道,我们不需要考虑arr_pa里面的字符串是不是以”\” 结尾,这的确提供了方便,而且这也是很多人喜欢使用Path.Combine的一个原因,但是仅此而已。
Path.Combine 虽然解决了路径连接问题,但是由于很多人片面的去理解它,所有它非常容易造成错误的应用,要想用好Path.Combine 并非易事,下面我会列举几个实例来说明这点。
第一个:当path2 是相对路径的时候,返回的是path2,path1会被丢弃。
看一下下面的代码:
public static void Main()
{
string[] arr_pa = { @"c:\abc\", @"c:\abc" };
string[] arr_pb = { @"\test.txt", @"/test.txt", @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, pb));
}
}
}
你知道这段代码输出什么吗?
这段代码的输出如下:

可以看到对于”/test.txt” 和”\test.txt” ,Path.Combine 认为path2是相对路径,所以直接返回path2.。
第二点:路径是驱动器,返回的结果不正确
public static void Main()
{
string[] arr_pa = { @"c:", @"c:\" };
string[] arr_pb = { @"\test.txt", @"/test.txt", @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, pb));
}
}
}
输出结果是:

可以看到,如果path1 是” C:”的话,那么Path.Combine结果就是不正确的。
第三点:无法连接http路径
除了连接本地路路径之外,有的时候,也需要拼接http链接地址,可惜的是System.IO.Path.Combine却无法拼接http地址。
将arr_pa 修改为
string[] arr_pa = { @"http://www.Test.com/", @"http://www.Test.com" };
结果如下:

在这里就没有什么技巧了,纯粹的死记硬背,
记住,只有

才会产生正确的解。
如果你将代码修改为:
public static void Main()
{
string[] arr_pa = { @"http://www.Test.com/", @"http://www.Test.com" };
string[] arr_pb = { @"\test.txt", @"/test.txt", @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, "def", pb));
}
}
}
那么无论怎样,你都无法得到正确的结果:

正是因为上述的几点不足,导致Path.Combine 很难用,这也是有一部分人选择使用String.Format 的原因了。
The answer is to use the System.Uri-constructor to combine the URL:
public static Uri CombineUri(string baseUri, string relativeOrAbsoluteUri) {
return new Uri(new Uri(baseUri), relativeOrAbsoluteUri);
}
public static string CombineUriToString(string baseUri, string relativeOrAbsoluteUri) {
return new Uri(new Uri(baseUri), relativeOrAbsoluteUri).ToString();
}
..
// Results in "http://www.my.domain/relative/path"
var a = CombineUriToString("http://www.my.domain/", "relative/path");
// Results in "http://www.my.domain/absolute/path"
var b = CombineUriToString("http://www.my.domain/something/other", "/absolute/path");
C# Path.Combine 缺陷(http路径用Uri类)的更多相关文章
- C# Path 有关于文件路径等问题类(转)
C# Path 标签:C#, Path C-Sharp 0 Path handles file path processing. The .NET Framework provides effect ...
- 关于程序路径Path.Combine以及AppDomain.CurrentDomain.BaseDirectory
关于程序路径 LucenePath:@(System.Configuration.ConfigurationManager.AppSettings["LucenePath"])&l ...
- Path.Combine 合并两个路径字符串,会出现的问题
Path.Combine(path1,path2) 1.如果path2字符串,以 \ 或 / 开头,则直接返回 path2
- C# Path.Combine 方法的用法
C# Path.Combine 方法的用法 *.注意: string filePath3= Path.Combine(string path1,string path2): 情况一: path2中 ...
- 基于用Path.Combine的优化
Path.Combine: 什么时候会用到Path.Combine呢?,当然是连接路径字符串的时候! 所以下面的代码可以完美的工作: public static void Main() { strin ...
- 使用System.IO.Combine(string path1, string path2, string path3)四个参数的重载函数提示`System.IO.Path.Combine(string, string, string, string)' is inaccessible due to its protection level
今天用Unity5.5.1开发提取Assets目录的模块,使用时采用System.IO.Path.Combine(string, string, string, string)函数进行路径生成 明明是 ...
- c# Path.Combine
Path.Combine: c#获取当前项目路径 : //获取包含当前执行的代码的程序集的加载文件的完整路径 var appPath = System.IO.Path.GetDirectoryName ...
- Path.Combine Method
https://docs.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=netframework-4.8#System_IO_P ...
- Path.Combine(
// 获取程序的基目录. var dir1 = System.AppDomain.CurrentDomain.BaseDirectory; // 获取模块的完整路径. var dir2 = Syste ...
随机推荐
- 强大的Visual Studio插件CodeRush全新发布v19.2,助力VS开发
CodeRush是一个强大的Visual Studio .NET 插件,它利用整合技术,通过促进开发者和团队效率来提升开发者体验.CodeRush能帮助你以极高的效率创建和维护源代码.Consume- ...
- SSM框架中使用日志框架
在 pom,xml 配置 Log4j jar 添加一个 mybatis_log.xml 文件 完整配置信息 <?xml version="1.0" encoding=&quo ...
- Badboy + JMeter性能测试(转)
1. 软件介绍 1.1 Badboy Badboy是用来录制操作过程的,它录制的结果是被jmeter做并发测试的素材使用. 下载网址:http://www.badboy.com.au/ 1.2下 ...
- [Algorithm] Finding Prime numbers - Sieve of Eratosthenes
Given a number N, the output should be the all the prime numbers which is less than N. The solution ...
- ipvsadm命令用法
ipvsadm命令选项 -A 添加虚拟服务器 -E 修改虚拟服务器 -D ...
- APP技术选型
- 三元环HDU 6184
HDU - 6184 C - Counting Stars 题目大意:有n个点,m条边,问有一共有多少个‘structure’也就是满足V=(A,B,C,D) and E=(AB,BC,CD,DA,A ...
- 两篇将rf和boosting方法用在搜索排序上的paper
在网上看到关于排序学习的早期文章,这两篇文章大致都使用了Random Forest和Boosting方法. 一.paper 1.Web-Search Ranking with Initialized ...
- Java线程之如何分析死锁及避免死锁
什么是死锁 java中的死锁是一种编程情况,其中两个或多个线程被永久阻塞,Java死锁情况出现至少两个线程和两个或更多资源. 在这里,我们将写了一个简单的程序,它将导致java死锁场景,然后我们将分析 ...
- Android_(自动化)获取手机存储卡的容量
手机上的存储卡是可以随时插拔的,每次插拔时会像操作系统总发送Action广播事件. 使用StatFs文件系统来获取MicroSD存储卡的剩余容量,在使用前先判断是否插入了存储卡,如果不存在则不于计算 ...