C# Path.Combine 缺陷(http路徑用Uri類)

Path.Combine:dom

image

何時會用到Path.Combine呢?,固然是鏈接路徑字符串的時候!spa

因此下面的代碼能夠完美的工做:code

public static void Main()orm

{rem

    string[] arr_pa = { @"c:\abc\", @"c:\abc" };字符串

    string[] arr_pb = { @"test.txt" };rpc

    foreach (string pa in arr_pa)get

    {string

        foreach (string pb in arr_pb)it

        {

            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 的緣由了。

 

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