0

I have 2 file paths:

absolute:

/Content/assets2/otherfolder/another/this/

relative:

../../../../assets/img/logo.gif

when I do Path.Combine(absolute, relative) I get:

/Content/assets2/otherfolder/another/this/../../../../assets/img/logo.gif

which works fine, but what I want to get:

/Content/assets/img/logo.gif

I need a Regex or code that removes the "../" with the corresponding folder:

/Content/assets2/otherfolder/another/this/../../../../assets/img/logo.gif
/Content/assets2/otherfolder/another/../../../assets/img/logo.gif
/Content/assets2/otherfolder/../../assets/img/logo.gif
/Content/assets2/../assets/img/logo.gif

finally into:

/Content/assets/img/logo.gif
Braian
  • 93
  • 7
  • I found how: http://stackoverflow.com/questions/670566/path-combine-absolute-with-relative-path-strings?rq=1 – Braian Nov 28 '13 at 20:47
  • Will one of the next close voters mark as duplicate http://stackoverflow.com/questions/670566/path-combine-absolute-with-relative-path-strings?rq=1 – rene Dec 09 '13 at 13:28

1 Answers1

7

You don't need a Regex for that. Actually, I think it would be impossible to define in a reliable way in Regex. Instead, use Path.GetFullPath:

string combined = Path.Combine(path1, path2);
string prettyPath = Path.GetFullPath(combined);
driis
  • 161,458
  • 45
  • 265
  • 341