There\'s a part in my apps that displays the file path loaded by the user through OpenFileDialog. It\'s taking up too much space to display the whole path, but I don\'t want
here's mine:
public static string RelativePathTo(this System.IO.DirectoryInfo @this, string to)
{
var rgFrom = @this.FullName.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
var rgTo = to.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
var cSame = rgFrom.TakeWhile((p, i) => i < rgTo.Length && string.Equals(p, rgTo[i])).Count();
return Path.Combine(
Enumerable.Range(0, rgFrom.Length - cSame)
.Select(_ => "..")
.Concat(rgTo.Skip(cSame))
.ToArray()
);
}