To my surprise, this code does not produce expected results:
var basePath = @"\\server\BaseFolder";
var relativePath = @"\My\Relative\Folder";
var combinedPath = Path.Combine(basePath, relativePath);
The result is \My\Relative\Folder
instead of the expected \\server\BaseFolder\My\Relative\Folder
.
Why is this? What's the best way to combine relative paths that may or may not have a slash in them?
EDIT: I'm aware that I can just do string manipulation on relativePath to detect and remove a starting slash. Is there a safer way of doing this (I thought Path.Combine
was supposed to be the safe way) that will account for backslashes and frontslashes?