开发者

Why Path.Combine doesn't add the Path.DirectorySeparatorChar after the drive designator?

开发者 https://www.devze.com 2022-12-08 00:58 出处:网络
var actual = Path.Combine(\"c:\", \"filename\"); var expected = @\"c:\\filename\"; Assert.AreEqual(exp开发者_运维问答ected, actual);
var actual = Path.Combine("c:", "filename");
var expected = @"c:\filename";
Assert.AreEqual(exp开发者_运维问答ected, actual);

Result

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

Why?


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.

This behaviour is described here in MSDN


MSDN doesn't seem to explain why, but does provide documentation on what you're seeing:

Path.Combine(string path1, string path2)

If path1 is not a drive reference (that is, "C:" or "D:") and does not end with a valid separator character as defined in DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar, DirectorySeparatorChar is appended to path1 before concatenation.

0

精彩评论

暂无评论...
验证码 换一张
取 消