Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing ../ in the middle of a relative path

Tags:

c#

I want to get from this

"../lib/../data/myFile.xml"

to this

"../data/myFile.xml"

I guess I could do it by manipulating the string, searching for "../" and canceling them out with the preceding folders but I was looking for an already existing C# solution.

Tried instantiating an Uri from this string and going back toString(). Didn't help. It leaves the string unchanged.

like image 571
Air Avatar asked Mar 26 '14 15:03

Air


1 Answers

You can always try to use:

Path.GetFullPath("../lib/../data/myFile.xml")

It behaves as you want with absolute paths but you might end up with strange behaviors with relative paths since it always bases itself from the current working directory. For instance:

Path.GetFullPath("/lib/../data/myFile.xml")   // C:\data\myFile.xml
Path.GetFullPath("../lib/../data/myFile.xml") // C:\Program Files (x86)\data\myFile.xml
like image 102
Etienne Maheu Avatar answered Sep 19 '22 19:09

Etienne Maheu