Quantcast
Channel: How to get relative path from absolute path - Stack Overflow
Viewing all articles
Browse latest Browse all 25

Answer by excanoe for How to get relative path from absolute path

$
0
0

This should work:

private string rel(string path) {  string[] cwd  = new Regex(@"[\\]").Split(Directory.GetCurrentDirectory());  string[] fp   = new Regex(@"[\\]").Split(path);  int common = 0;  for (int n = 0; n < fp.Length; n++) {    if (n < cwd.Length && n < fp.Length && cwd[n] == fp[n]) {      common++;    }  }  if (common > 0) {    List<string> rp = new List<string>();    for (int n = 0; n < (cwd.Length - common); n++) {      rp.Add("..");    }    for (int n = common; n < fp.Length; n++) {      rp.Add(fp[n]);    }    return String.Join("/", rp.ToArray());  } else {    return String.Join("/", fp);  }}

Viewing all articles
Browse latest Browse all 25

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>