在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()不是这样工作的……
当前回答
这个\表示“当前驱动器的根目录”。在你的例子中,它指的是当前驱动器根目录中的“test”文件夹。所以,这可以等于"c:\test"。
其他回答
由于不知道实际的细节,我猜测它尝试像连接相对uri那样连接。例如:
urljoin('/some/abs/path', '../other') = '/some/abs/other'
这意味着当您使用前面的斜杠连接路径时,实际上是将一个基底连接到另一个基底,在这种情况下,第二个基底优先。
正如Ryan所提到的,它正在做文档中所说的事情。
根据DOS时间,区分当前磁盘和当前路径。 \是根路径,但用于当前磁盘。
对于每个“磁盘”都有一个单独的“当前路径”。 如果您使用cd D:更改磁盘,则不会将当前路径更改为D:\,而是更改为:“D:\whatever\was\the\last\path\access \on\this\disk”…
因此,在windows中,文字@“\x”意味着:“CURRENTDISK:\x”。 因此路径。Combine(@"C:\x", @"\y")的第二个参数是根路径,不是相对路径,虽然不在已知磁盘中… 由于不知道哪个可能是«当前磁盘»,python返回“\\y”。
>cd C:
>cd \mydironC\apath
>cd D:
>cd \mydironD\bpath
>cd C:
>cd
>C:\mydironC\apath
这个\表示“当前驱动器的根目录”。在你的例子中,它指的是当前驱动器根目录中的“test”文件夹。所以,这可以等于"c:\test"。
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.
这段代码应该做到这一点:
string strFinalPath = string.Empty;
string normalizedFirstPath = Path1.TrimEnd(new char[] { '\\' });
string normalizedSecondPath = Path2.TrimStart(new char[] { '\\' });
strFinalPath = Path.Combine(normalizedFirstPath, normalizedSecondPath);
return strFinalPath;