为什么我不喜欢用Path.Combine

简介:

Path.Combine:

image

什么时候会用到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));

        }

    }

}

结果如下:

image

当然你也可以:

Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, "def", pb));

结果是:

image

从这个例子可以知道,我们不需要考虑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));

        }

    }

}

你知道这段代码输出什么吗?

这段代码的输出如下:

image

可以看到对于”/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));

        }

    }

}

输出结果是:

image

可以看到,如果path1 是” C:”的话,那么Path.Combine结果就是不正确的。

 

第三点:无法连接http路径

除了连接本地路路径之外,有的时候,也需要拼接http链接地址,可惜的是System.IO.Path.Combine却无法拼接http地址。

arr_pa 修改为

string[] arr_pa = { @"http://www.Test.com/"@"http://www.Test.com" };

结果如下:

image

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

记住,只有

image

才会产生正确的解。

如果你将代码修改为:

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));

        }

    }

}

那么无论怎样,你都无法得到正确的结果:

image

正是因为上述的几点不足,导致Path.Combine 很难用,这也是有一部分人选择使用String.Format 的原因了。

 

当然,我写了一个MyPath.Combine的方法,可以解决上面的问题。

MyPath

 

使用也比较简单

例如:

public static void Main()

{

    string[] arr_pa = { @"c:\abc\"@"c:\abc"@"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, MyPath.Combine(pa, pb));

        }

    }

}

结果如下:

image

当然,你也可以这样:

Console.WriteLine("{0}+{1}+{2}+{3}={4}",

                        pa, "p2""\\p3/", pb, MyPath.Combine(pa, "p2""\\p3/", pb));

输出如下:

image

 

最后如果你不用Path.Combine,那么还可以选择string.Format






本文转自LoveJenny博客园博客,原文链接:http://www.cnblogs.com/LoveJenny/archive/2012/03/05/2381094.html,如需转载请自行联系原作者
目录
相关文章
|
11月前
|
数据可视化 数据处理
解决 FAILED: UDFArgumentException explode() takes an array or a map as a parameter 并理解炸裂函数和侧视图
解决 FAILED: UDFArgumentException explode() takes an array or a map as a parameter 并理解炸裂函数和侧视图
72 0
|
12月前
InvalidJobConfException: Output directory not set
InvalidJobConfException: Output directory not set
52 0
|
NoSQL MongoDB 数据库
DeprecationWarning: count is deprecated. Use Collection.count_documents instead
当我使用pymongo查询出对应的cursor(find出的document的迭代器),然后查看查询出数据的数量时使用如下代码: ```python db = MongoClient(host='192.168.1.3', port=27017) # dbname为操作的数据库名称,collectionname为操作的集合名称
332 0
|
分布式计算 Java 5G
spark异常:missing an output location for shuffle 0
spark异常:missing an output location for shuffle 0
485 0
|
分布式计算 Spark Go
|
JavaScript Windows 移动开发
Isodraft output attributes
Isodraft output attributes eryar@163.com AVEVA Isodraft中有许多开关用于配置管道ISO图的图面内容,这些配置都可以从文档《Isodraft Reference Manual》中找到。
1133 0