国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

基于為何我不喜歡用Path.Combine的詳解

2020-01-24 03:21:37
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Path.Combine:

image

什么時(shí)候會(huì)用到Path.Combine呢?,當(dāng)然是連接路徑字符串的時(shí)候!

所以下面的代碼可以完美的工作:

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

        }

    }

}

結(jié)果如下:

image

當(dāng)然你也可以:

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

結(jié)果是:

image

從這個(gè)例子可以知道,我們不需要考慮arr_pa里面的字符串是不是以”/” 結(jié)尾,這的確提供了方便,而且這也是很多人喜歡使用Path.Combine的一個(gè)原因,但是僅此而已。

Path.Combine 雖然解決了路徑連接問(wèn)題,但是由于很多人片面的去理解它,所有它非常容易造成錯(cuò)誤的應(yīng)用,要想用好Path.Combine 并非易事,下面我會(huì)列舉幾個(gè)實(shí)例來(lái)說(shuō)明這點(diǎn)。

第一個(gè):當(dāng)path2 是相對(duì)路徑的時(shí)候,返回的是path2,path1會(huì)被丟棄。

看一下下面的代碼:

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

可以看到對(duì)于”/test.txt” ”/test.txt” ,Path.Combine 認(rèn)為path2是相對(duì)路徑,所以直接返回path2.

第二點(diǎn):路徑是驅(qū)動(dòng)器,返回的結(jié)果不正確

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

        }

    }

}

輸出結(jié)果是:

image

可以看到,如果path1 是” C:”的話,那么Path.Combine結(jié)果就是不正確的。

第三點(diǎn):無(wú)法連接http路徑

除了連接本地路路徑之外,有的時(shí)候,也需要拼接http鏈接地址,可惜的是System.IO.Path.Combine卻無(wú)法拼接http地址。

arr_pa 修改為

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

結(jié)果如下:

image

在這里就沒(méi)有什么技巧了,純粹的死記硬背,

記住,只有

image

才會(huì)產(chǎn)生正確的解。

如果你將代碼修改為:

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

        }

    }

}

那么無(wú)論怎樣,你都無(wú)法得到正確的結(jié)果:

image

正是因?yàn)樯鲜龅膸c(diǎn)不足,導(dǎo)致Path.Combine 很難用,這也是有一部分人選擇使用String.Format 的原因了。

當(dāng)然,我寫(xiě)了一個(gè)MyPath.Combine的方法,可以解決上面的問(wèn)題。

復(fù)制代碼 代碼如下:

class MyPath
    {
        public static string Combine(params string[] paths)
        {
            if (paths.Length == 0)
            {
                throw new ArgumentException("please input path");
            }
            else
            {
                StringBuilder builder = new StringBuilder();
                string spliter = "http://";

                string firstPath = paths[0];

                if (firstPath.StartsWith("HTTP", StringComparison.OrdinalIgnoreCase))
                {
                    spliter = "/";
                }

                if (!firstPath.EndsWith(spliter))
                {
                    firstPath = firstPath + spliter;
                }
                builder.Append(firstPath);

                for (int i = 1; i < paths.Length; i++)
                {
                    string nextPath = paths[i];
                    if (nextPath.StartsWith("/") || nextPath.StartsWith("http://"))
                    {
                        nextPath = nextPath.Substring(1);
                    }

                    if (i != paths.Length - 1)//not the last one
                    {
                        if (nextPath.EndsWith("/") || nextPath.EndsWith("http://"))
                        {
                            nextPath = nextPath.Substring(0, nextPath.Length - 1) + spliter;
                        }
                        else
                        {
                            nextPath = nextPath + spliter;
                        }
                    }

                    builder.Append(nextPath);
                }

                return builder.ToString();
            }
        }
    }


使用也比較簡(jiǎn)單

例如:

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

        }

    }

}

結(jié)果如下:

image

當(dāng)然,你也可以這樣:

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

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

輸出如下:

image

最后如果你不用Path.Combine,那么還可以選擇string.Format

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 若羌县| 商城县| 富源县| 玉树县| 乡宁县| 四平市| 北流市| 集安市| 景洪市| 乌拉特前旗| 凤冈县| 临泉县| 丹寨县| 沁源县| 邹平县| 石阡县| 开阳县| 沅江市| 湘潭市| 泰宁县| 象山县| 仙桃市| 渭源县| 华池县| 通江县| 城固县| 芒康县| 溧水县| 措勤县| 磐石市| 溆浦县| 延安市| 从化市| 邻水| 行唐县| 凉山| 邓州市| 河曲县| 阜康市| 张家港市| 达拉特旗|