.NET/C#: Drives, Directories, Paths and Filenames in Windows is hard, let alone cross platform (: via: .net – Why Path.Combine doesn’t add the Path.DirectorySeparatorChar after the drive designator? – Stack Overflow)
Posted by jpluimers on 2012/11/08
Handling names of drives and paths (directories, filenames) is hard in Windows, as both C:myfile.ext
and C:\myfile.ext
are valid – but potentially different – filenames, C
is a valid driveletter, C:
and C:\
are valid – but also potentially different – directory names.
This leads into confusion as how Path.Combine behaves.
Part of the confusion comes from the meaning of the absence or presence of the leading DirectorySeparatorChar as explained by user Peter van der Heijden:
C:filename
is a valid path and is different fromC:\filename
.C:filename
is the file filename in the current directory on theC:
drive whereasC:\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
Another oddity is that Path.Combine will only use the drive portion of the left argument when the right argument contains an absolute path.
If you understand the above, then dealing with cross platform directory and path separators, spaces in filenames and UNC path names are peanuts (:
–jeroen
Leave a Reply