为什么Path.Combine的驱动器盘符后不加Path.DirectorySeparatorChar?驱动器、不加、Combine、Path

2023-09-04 00:13:59 作者:玩世不恭浪荡少爷

var actual = Path.Combine("c:", "filename");
var expected = @"c:\filename";
Assert.AreEqual(expected, actual);

结果

{Assert.AreEqual failed. Expected:<c:\filename>. Actual:<c:filename>.

为什么?

推荐答案

C:文件名是一个有效的路径和不同于 C:\文件名 C:文件名是文件中的 c把当前目录下的文件名驱动而 C:\文件名文件名在该驱动器的根目录下的文件。显然,他们想保留指的是当前目录中的一些驱动器上的功能。

C:filename is a valid path and is different from C:\filename. C:filename is the file filename in the current directory on the C: drive whereas C:\filename is the file filename in the root of that drive. Apparently they wanted to keep the functionality of refering to the current directory on some drive.

此行​​为被描述这里在MSDN

This behaviour is described here in MSDN