a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

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

2020-01-24 03:21:37
字體:
來源:轉載
供稿:網友

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的方法,可以解決上面的問題。

復制代碼 代碼如下:

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


使用也比較簡單

例如:

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", "http://p3/", pb, MyPath.Combine(pa, "p2", "http://p3/", pb));

輸出如下:

image

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一级特黄色大片 | 国产精品1页 | 亚洲视频欧美视频 | 欧美日韩高清丝袜 | 91av国产在线视频 | 欧美日韩一二三 | 亚洲精品一 | 国产精品毛片久久久久久 | 成人欧美一区二区三区在线观看 | 毛片av在线| 精品视频免费在线 | 久久一区二区视频 | 香蕉久久一区二区不卡无毒影院 | 国产精品久久久久久久久久妞妞 | 午夜免费视频 | 国产成人免费av一区二区午夜 | 欧美日韩成人在线播放 | 99热日本 | 午夜妇女aaaa区片 | 欧美日本久久 | 国产视频中文字幕 | www.日韩| 娇妻被朋友调教成玩物 | 国产精品成人在线 | 久久亚洲高清 | 亚洲国产视频一区 | 久久精品亚洲精品 | 亚洲蜜臀av乱码久久精品蜜桃 | 午夜激情视频在线观看 | 久久69精品久久久久久久电影好 | 在线免费视频一区 | www.日本三级 | 日韩五码在线 | 国产偷国产偷精品高清尤物 | 欧美精品一二三区 | 欧美精品免费在线 | 一区二区三区久久 | 亚洲人成人一区二区在线观看 | 国产人成免费视频 | 久久69国产一区二区蜜臀 | 中文字幕在线视频免费观看 |