PathUtils.cs 4.5 KB

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