Friday, May 5, 2017

Path.Combine and Path.GetFullPath

I bumped into a case in which I need to resolve a relative path to a file, but the problem is it doesn't resolve as expected when Path.GetFullPath is used.

For example:
path1 = "C:\file\"
path2 = "..\test.txt"
Path.Combine produces "C:\file\..\test.txt"
Path.GetFullPath((New Uri(Path.Combine(path1,path2))).LocalPath) produces "C:\test.txt" which is correct.

The problem starts when I realized another slash was accidentally appended to the end of path1.

path1 = "C:\file\"
path2 = "..\test.txt"
Path.Combine produces "C:\file\\..\test.txt"
Path.GetFullPath((New Uri(Path.Combine(path1,path2))).LocalPath) produces "C:\file\test.txt" which is correct but unexpected.


No comments:

Post a Comment