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); }}