From the Immediate Window in Visual Studio:
> Path.Combine(@'C:\x', 'y') 'C:\\x\\y' > Path.Combine(@'C:\x', @'\y') '\\y'
It seems that they should both be the same.
The old FileSystemObject.BuildPath() didn’t work this way…
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is kind of a philosophical question (which perhaps only Microsoft can truly answer), since it’s doing exactly what the documentation says.
System.IO.Path.Combine
‘If path2 contains an absolute path, this method returns path2.’
Here’s the actual Combine method from the .NET source. You can see that it calls CombineNoChecks, which then calls IsPathRooted on path2 and returns that path if so:
I don’t know what the rationale is. I guess the solution is to strip off (or Trim) DirectorySeparatorChar from the beginning of the second path; maybe write your own Combine method that does that and then calls Path.Combine().