在Visual Studio的即时窗口中:
> Path.Combine(@"C:\x", "y")
"C:\\x\\y"
> Path.Combine(@"C:\x", @"\y")
"\\y"
看来它们应该是一样的。
旧的FileSystemObject.BuildPath()不是这样工作的……
在Visual Studio的即时窗口中:
> Path.Combine(@"C:\x", "y")
"C:\\x\\y"
> Path.Combine(@"C:\x", @"\y")
"\\y"
看来它们应该是一样的。
旧的FileSystemObject.BuildPath()不是这样工作的……
当前回答
这实际上是有意义的,在某种程度上,考虑到(相对)路径通常是如何处理的:
string GetFullPath(string path)
{
string baseDir = @"C:\Users\Foo.Bar";
return Path.Combine(baseDir, path);
}
// Get full path for RELATIVE file path
GetFullPath("file.txt"); // = C:\Users\Foo.Bar\file.txt
// Get full path for ROOTED file path
GetFullPath(@"C:\Temp\file.txt"); // = C:\Temp\file.txt
真正的问题是:为什么以“\”开头的路径被认为是“根路径”?这对我来说也很新鲜,但它在Windows上是这样工作的:
new FileInfo("\windows"); // FullName = C:\Windows, Exists = True
new FileInfo("windows"); // FullName = C:\Users\Foo.Bar\Windows, Exists = False
其他回答
这实际上是有意义的,在某种程度上,考虑到(相对)路径通常是如何处理的:
string GetFullPath(string path)
{
string baseDir = @"C:\Users\Foo.Bar";
return Path.Combine(baseDir, path);
}
// Get full path for RELATIVE file path
GetFullPath("file.txt"); // = C:\Users\Foo.Bar\file.txt
// Get full path for ROOTED file path
GetFullPath(@"C:\Temp\file.txt"); // = C:\Temp\file.txt
真正的问题是:为什么以“\”开头的路径被认为是“根路径”?这对我来说也很新鲜,但它在Windows上是这样工作的:
new FileInfo("\windows"); // FullName = C:\Windows, Exists = True
new FileInfo("windows"); // FullName = C:\Users\Foo.Bar\Windows, Exists = False
这段代码应该做到这一点:
string strFinalPath = string.Empty;
string normalizedFirstPath = Path1.TrimEnd(new char[] { '\\' });
string normalizedSecondPath = Path2.TrimStart(new char[] { '\\' });
strFinalPath = Path.Combine(normalizedFirstPath, normalizedSecondPath);
return strFinalPath;
In my opinion this is a bug. The problem is that there are two different types of "absolute" paths. The path "d:\mydir\myfile.txt" is absolute, the path "\mydir\myfile.txt" is also considered to be "absolute" even though it is missing the drive letter. The correct behavior, in my opinion, would be to prepend the drive letter from the first path when the second path starts with the directory separator (and is not a UNC path). I would recommend writing your own helper wrapper function which has the behavior you desire if you need it.
这是一个哲学问题(也许只有微软能真正回答这个问题),因为它正在做文档中所说的事情。
System.IO.Path.Combine
如果path2包含一个绝对路径,则此方法返回path2。
下面是. net源代码中的实际Combine方法。你可以看到它调用了CombineNoChecks,然后在path2上调用ispathooot,如果是的话返回该路径:
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从第二个路径的开始;也许你可以编写自己的Combine方法,然后调用Path.Combine()。
这个\表示“当前驱动器的根目录”。在你的例子中,它指的是当前驱动器根目录中的“test”文件夹。所以,这可以等于"c:\test"。