I know that this question was answered many times and the solution that I have works almost in all cases except for one.
The code in question is this:
Path.GetFullPath(Path.Combine(rootFolder, relativeFilePath))
It works great most of the time even with messy relative paths, UNC and absolute paths as shown below:
// These results are OK
Path.GetFullPath(Path.Combine(@"a:\b\c", @"e.txt")); // Returns: "a:\b\c\e.txt"
Path.GetFullPath(Path.Combine(@"a:\b\c", @"..\e.txt")); // Returns: "a:\b\e.txt"
Path.GetFullPath(Path.Combine(@"a:\b\c", @"d:\e.txt")); // Returns: "d:\e.txt"
However in this case it does not work as expected:
Path.GetFullPath(Path.Combine(@"a:\b\c", @"\e.txt"))
Expected result is "a:\e.txt" but the code returns "c:\e.txt". "c:" is the current drive so it solves the part of the puzzle but why is this happening? Is there another way to get the full path that works in all cases?
Edit: Based on info from answers here is the solution that works. May need some null checking and alternative directory separator char checking though:
var rootDir = @"a:\b\c";
var filePath = @"\e.txt";
var result = (Path.IsPathRooted(filePath) &&
Path.GetPathRoot(filePath) == Path.DirectorySeparatorChar.ToString()) ?
Path.GetFullPath(Path.Combine(Path.GetPathRoot(rootDir), filePath.Substring(1))) :
Path.GetFullPath(Path.Combine(rootDir, filePath));