首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么Path.Combine不能正确连接以Path.DirectorySeparatorChar开头的文件名?

为什么Path.Combine不能正确连接以Path.DirectorySeparatorChar开头的文件名?
EN

Stack Overflow用户
提问于 2008-09-09 23:05:07
回答 13查看 117.1K关注 0票数 212

在Visual Studio的即时窗口中:

代码语言:javascript
复制
> Path.Combine(@"C:\x", "y")
"C:\\x\\y"
> Path.Combine(@"C:\x", @"\y")
"\\y"

看起来它们应该是一样的。

EN

回答 13

Stack Overflow用户

回答已采纳

发布于 2008-09-09 23:16:02

这是一个哲学问题(也许只有微软才能真正回答),因为它完全按照文档所说的去做。

System.IO.Path.Combine

如果path2包含绝对路径,则此方法返回path2。

来自.NET源代码的Here's the actual Combine method。您可以看到它调用了CombineNoChecks,然后在path2上调用IsPathRooted并返回该路径:

代码语言:javascript
复制
public static String Combine(String path1, String path2) {
    if (path1==null || path2==null)
        throw new ArgumentNullException((path1==null) ? "path1" : "path2");
    Contract.EndContractBlock();
    CheckInvalidPathChars(path1);
    CheckInvalidPathChars(path2);

    return CombineNoChecks(path1, path2);
}

internal static string CombineNoChecks(string path1, string path2)
{
    if (path2.Length == 0)
        return path1;

    if (path1.Length == 0)
        return path2;

    if (IsPathRooted(path2))
        return path2;

    char ch = path1[path1.Length - 1];
    if (ch != DirectorySeparatorChar && ch != AltDirectorySeparatorChar &&
            ch != VolumeSeparatorChar) 
        return path1 + DirectorySeparatorCharAsString + path2;
    return path1 + path2;
}

我不知道理由是什么。我猜解决方案是从第二个路径的开头去掉(或修剪) DirectorySeparatorChar;也许可以编写自己的组合方法来实现这一点,然后调用Path.Combine()。

票数 228
EN

Stack Overflow用户

发布于 2015-06-30 14:52:22

我想解决这个问题:

代码语言:javascript
复制
string sample1 = "configuration/config.xml";
string sample2 = "/configuration/config.xml";
string sample3 = "\\configuration/config.xml";

string dir1 = "c:\\temp";
string dir2 = "c:\\temp\\";
string dir3 = "c:\\temp/";

string path1 = PathCombine(dir1, sample1);
string path2 = PathCombine(dir1, sample2);
string path3 = PathCombine(dir1, sample3);

string path4 = PathCombine(dir2, sample1);
string path5 = PathCombine(dir2, sample2);
string path6 = PathCombine(dir2, sample3);

string path7 = PathCombine(dir3, sample1);
string path8 = PathCombine(dir3, sample2);
string path9 = PathCombine(dir3, sample3);

当然,所有路径1-9的末尾都应该包含一个等价的字符串。下面是我想出来的PathCombine方法:

代码语言:javascript
复制
private string PathCombine(string path1, string path2)
{
    if (Path.IsPathRooted(path2))
    {
        path2 = path2.TrimStart(Path.DirectorySeparatorChar);
        path2 = path2.TrimStart(Path.AltDirectorySeparatorChar);
    }

    return Path.Combine(path1, path2);
}

我还认为这个字符串处理必须手动完成是相当烦人的,我对此背后的原因很感兴趣。

票数 28
EN

Stack Overflow用户

发布于 2008-09-09 23:17:11

这是用于Path.Combine方法的.NET Reflector的反汇编代码。检查IsPathRooted函数。如果第二个路径是根路径(以DirectorySeparatorChar开头),则按原样返回第二个路径。

代码语言:javascript
复制
public static string Combine(string path1, string path2)
{
    if ((path1 == null) || (path2 == null))
    {
        throw new ArgumentNullException((path1 == null) ? "path1" : "path2");
    }
    CheckInvalidPathChars(path1);
    CheckInvalidPathChars(path2);
    if (path2.Length == 0)
    {
        return path1;
    }
    if (path1.Length == 0)
    {
        return path2;
    }
    if (IsPathRooted(path2))
    {
        return path2;
    }
    char ch = path1[path1.Length - 1];
    if (((ch != DirectorySeparatorChar) &&
         (ch != AltDirectorySeparatorChar)) &&
         (ch != VolumeSeparatorChar))
    {
        return (path1 + DirectorySeparatorChar + path2);
    }
    return (path1 + path2);
}


public static bool IsPathRooted(string path)
{
    if (path != null)
    {
        CheckInvalidPathChars(path);
        int length = path.Length;
        if (
              (
                  (length >= 1) &&
                  (
                      (path[0] == DirectorySeparatorChar) ||
                      (path[0] == AltDirectorySeparatorChar)
                  )
              )

              ||

              ((length >= 2) &&
              (path[1] == VolumeSeparatorChar))
           )
        {
            return true;
        }
    }
    return false;
}
票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档