来自Visual Studio中的Immediate Window:
> Path.Combine(@"C:\x", "y")
"C:\\x\\y"
> Path.Combine(@"C:\x", @"\y")
"\\y"
看起来它们应该是一样的.
旧的FileSystemObject.BuildPath()不是这样工作的...
来自Visual Studio中的Immediate Window:
> Path.Combine(@"C:\x", "y")
"C:\\x\\y"
> Path.Combine(@"C:\x", @"\y")
"\\y"
看起来它们应该是一样的.
旧的FileSystemObject.BuildPath()不是这样工作的...
这是一个哲学问题(也许只有微软才能真正回答),因为它完全按照文档所说的go 做.
如果path2包含绝对路径,此方法将返回path2.
Here's the actual Combine method美元.净来源.您可以看到它调用CombineNoChecks,然后在path2上调用IsPathRooted,并返回该路径(如果是):
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;
}
我不知道理由是什么.我猜解决方案是从第二条路径的开始剥离(或修剪)DirectorySpeparatorChar;也许可以编写自己的Combine方法来实现这一点,然后调用Path.Combine().