PathUtils.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. namespace MeloongCore;
  2. public static class PathUtils {
  3. #region 分隔符
  4. /// <summary>
  5. /// 确保路径的结尾包含文件夹分隔符。
  6. /// </summary>
  7. public static string WithSeparator(string folder) {
  8. return folder.EndsWithF(Path.DirectorySeparatorChar) ? folder : folder + Path.DirectorySeparatorChar;
  9. }
  10. /// <summary>
  11. /// 确保路径的结尾不包含文件夹分隔符。
  12. /// </summary>
  13. public static string WithoutSeparator(string folder) {
  14. return folder.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  15. }
  16. #endregion
  17. #region 长路径
  18. /// <summary>
  19. /// 若路径较长,则尽量将其转换为短路径。
  20. /// 若输入的是文件夹路径,不保证其结尾是否有文件夹分隔符。
  21. /// </summary>
  22. public static string ToShortPath(string fullName, bool keepFileName = false) {
  23. if (string.IsNullOrEmpty(fullName)) return fullName;
  24. fullName = PathUtils.WithoutLongPath( fullName.Replace('/', '\\'));
  25. if (fullName.Length <= 200) return fullName;
  26. // 保留文件名
  27. string pathToKeep = "";
  28. string pathToShorten = fullName;
  29. if (fullName.EndsWithF(".jar", true)) keepFileName = true; // jar 文件的文件名需要保留原样,否则会导致 Forge 1.20.1 无法通过文件名识别模块名
  30. if (keepFileName && FileUtils.Exists(fullName)) {
  31. pathToKeep = Path.GetFileName(fullName);
  32. pathToShorten = Path.GetDirectoryName(fullName);
  33. }
  34. // 逐级向上寻找已存在的文件夹,将不存在的部分挪到 suffix,不再缩短
  35. while (!DirectoryUtils.Exists(pathToShorten) && !FileUtils.Exists(pathToShorten)) { // 如果路径不存在
  36. string parentPath = Path.GetDirectoryName(pathToShorten);
  37. if (string.IsNullOrEmpty(parentPath) || parentPath == pathToShorten) return fullName; // 已经到达根目录,全都不存在,直接返回
  38. pathToKeep = Path.Combine(Path.GetFileName(pathToShorten), pathToKeep);
  39. pathToShorten = parentPath;
  40. }
  41. if (pathToShorten.Length <= 10) return fullName;
  42. // 缩短路径
  43. char[] buffer = new char[260];
  44. int result = GetShortPathNameW(pathToShorten, buffer, buffer.Length);
  45. if (result == 0) return fullName;
  46. string shortPath = new(buffer, 0, result);
  47. return Path.Combine(shortPath, pathToKeep);
  48. }
  49. [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
  50. private static extern int GetShortPathNameW(string lpszLongPath, [Out] char[] buffer, int cchBuffer);
  51. /// <summary>
  52. /// 将完整路径转换为以 \\?\ 开头的标准长路径格式。
  53. /// 这会去除路径末尾的分隔符,且将 / 替换为 \。
  54. /// </summary>
  55. public static string WithLongPath(string path) {
  56. if (string.IsNullOrWhiteSpace(path)) return path;
  57. if (path.StartsWithF(@"\\?\")) return path; // 已经是长路径
  58. path = WithoutSeparator(path).Replace('/', '\\'); // API 要求这个格式……
  59. if (path.StartsWithF(@"\\")) {
  60. return $@"\\?\UNC\{path.Substring(2)}";
  61. } else {
  62. return $@"\\?\{path}";
  63. }
  64. }
  65. /// <summary>
  66. /// 去除路径开头的 \\?\。
  67. /// </summary>
  68. public static string WithoutLongPath(string path) =>
  69. path.AfterLast(@"\\?\");
  70. #endregion
  71. }