PathHelper.cs 647 B

1234567891011121314151617181920212223242526
  1. using System;
  2. using System.IO;
  3. public static class PathHelper
  4. {
  5. public static string MakeRelativePath(string fromPath, string toPath)
  6. {
  7. var fromUri = new Uri(Path.GetFullPath(fromPath));
  8. var toUri = new Uri(Path.GetFullPath(toPath));
  9. if (fromUri.Scheme != toUri.Scheme)
  10. {
  11. return toPath;
  12. }
  13. var relativeUri = fromUri.MakeRelativeUri(toUri);
  14. var relativePath = Uri.UnescapeDataString(relativeUri.ToString());
  15. if (toUri.Scheme.Equals("file", StringComparison.InvariantCultureIgnoreCase))
  16. {
  17. relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
  18. }
  19. return relativePath;
  20. }
  21. }