Vincent
Vincent

Reputation: 125

Why Path.Combine() disrespects ".." (up one folder level)?

Using ".." to jump up one level is the oldest feature of file system navigation - EVERY command line utility support it! (yes, thanks to OS shell) But when you use Path.Combine(), it works as a stupid string concatenator! Sample:

Path.Combine(@"c:\abc\def", @"..\XYZ")

Obviously we expect c:\abc\XYZ, while real result is c:\abc\def\..\XYZ. What a reason to embed Combine() into Path class if it knows nothing about elementary folder syntax? Same way it could be coded as String.Combine().

Is there any .NET solution, which can handle path concatenation properly?

Upvotes: 2

Views: 94

Answers (1)

Vincent
Vincent

Reputation: 125

Fortunately there is simple solution - use standard function:

Path.GetFullPath(Path.Combine(@"C:\dir1\dir2", "..\dir3\file.txt")) => C:\dir1\dir3\file.txt

Upvotes: 1

Related Questions