【轉】 C# Path.Combine 缺陷


 

 

 

原文地址:https://www.jb51.net/article/36744.htm

 

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

結果

從這個例子可以知道,我們不需要考慮arr_pa里面的字符串是不是以”\” 結尾,這的確提供了方便,而且這也是很多人喜歡使用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 的原因了。

 

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 = "\\";
                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("\\"))
                    {
                        nextPath = nextPath.Substring(1);
                    }

                    if (i != paths.Length - 1)//not the last one
                    {
                        if (nextPath.EndsWith("/") || nextPath.EndsWith("\\"))
                        {
                            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));
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM