【转】 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