2

I'm reading a few relative paths from a file and I'm searching for a function which validates them / brings them into a unified format, like this:

./file1.bin         ->  file1.bin
././file2.bin       ->  file2.bin
file3.bin           ->  file3.bin
folder/f/file4.bin  ->  folder/f/file4.bin
a/b/c/../f.bin      ->  a/b/f.bin
./f/../data.bin     ->  data.bin
./f/../../../d.bin  ->  d.bin

Does such a function exist in C# or would I need to write it myself? While searching I've only found functions which do that conversation relative to a "main path", i. e., converting such a relative path into an absolute one. Or functions that handle / to \ conversation but not trimming the useless "/./" or "/../" blocks.

Florian Bach
  • 830
  • 7
  • 20
  • If you know how to do that "*relative to a main path*", use a main path of your choice and strip it after the conversion. – Amit May 21 '16 at 09:50
  • These functions still don't remove useless ".." and "." parts from the path. Also, then I'd need to take care of people "escaping" into my main path by using too many ".." folders. – Florian Bach May 21 '16 at 09:56
  • 1
    Your problem is basically undefined. `./f/../../../d.bin -> d.bin` doesn't follow any standard rules, so of course you have to do that yourself – Amit May 21 '16 at 10:17
  • http://stackoverflow.com/questions/126242/how-do-i-turn-a-relative-url-into-a-full-url – mohsen May 21 '16 at 10:28
  • 1
    Possible duplicate of [Convert relative path to full URL](http://stackoverflow.com/questions/17421120/convert-relative-path-to-full-url) – mohsen May 21 '16 at 10:29
  • "doesn't follow standard rules"? Isn't that basically what every archive format also does to stop archives from "escaping" by using multiple ".." folders? restrict the path to the archive root? I'd like to allow ".." to go up one level, but only up to the original root. – Florian Bach May 21 '16 at 10:43

0 Answers0