Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Result

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

Why?

like image 564
Jader Dias Avatar asked Oct 06 '09 20:10

Jader Dias


2 Answers

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

like image 157
heijp06 Avatar answered Nov 03 '22 15:11

heijp06


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.

like image 24
Gavin Miller Avatar answered Nov 03 '22 17:11

Gavin Miller