FileUtils.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. namespace FastReport.Utils
  6. {
  7. internal static class FileUtils
  8. {
  9. public static string GetRelativePath(string absPath, string baseDirectoryPath)
  10. {
  11. char[] separators = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
  12. baseDirectoryPath = Path.GetFullPath(baseDirectoryPath);
  13. absPath = Path.GetFullPath(absPath);
  14. baseDirectoryPath = baseDirectoryPath.TrimEnd(separators);
  15. string[] bPath = baseDirectoryPath.Split(separators);
  16. string[] aPath = absPath.Split(separators);
  17. int indx = 0;
  18. while (indx < Math.Min(bPath.Length, aPath.Length))
  19. {
  20. if (String.Compare(aPath[indx], bPath[indx], true) != 0)
  21. break;
  22. indx++;
  23. }
  24. // no matches, return absPath
  25. if (indx == 0)
  26. return absPath;
  27. string result = "";
  28. for (int i = indx; i < bPath.Length; i++)
  29. {
  30. result += ".." + Path.DirectorySeparatorChar;
  31. }
  32. result += String.Join(Path.DirectorySeparatorChar.ToString(), aPath, indx, aPath.Length - indx);
  33. return result;
  34. }
  35. }
  36. }