DirectoryUtils.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. namespace MeloongCore;
  2. public static class DirectoryUtils {
  3. /// <summary>
  4. /// 创建文件夹,或文件所在的文件夹。
  5. /// 文件夹已存在时不会抛出异常。
  6. /// </summary>
  7. public static void Create(string path, bool isFilePath = false) {
  8. if (isFilePath) path = PathUtils.RemoveFileName(path);
  9. if (Exists(path)) return;
  10. Logger.Trace($"新建文件夹:{path}");
  11. path = PathUtils.WithLongPath(path);
  12. Directory.CreateDirectory(path);
  13. }
  14. /// <summary>
  15. /// 判断文件夹是否存在。
  16. /// </summary>
  17. public static bool Exists(string path)
  18. => Directory.Exists(PathUtils.WithLongPath(path));
  19. /// <summary>
  20. /// 创建 <see cref="DirectoryInfo"/> 对象。
  21. /// </summary>
  22. public static DirectoryInfo GetInfo(string path)
  23. => new(PathUtils.WithLongPath(path));
  24. /// <summary>
  25. /// 返回指定路径下的所有文件,不以 \\?\ 开头。
  26. /// </summary>
  27. public static IEnumerable<string> GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
  28. if (!Exists(path)) return [];
  29. Logger.Trace($"枚举文件({searchPattern},递归:{!topDirectoryOnly}):{path}");
  30. return Directory.EnumerateFiles(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
  31. Select(PathUtils.WithoutLongPath);
  32. }
  33. /// <summary>
  34. /// 返回指定路径下的所有文件夹,不以分隔符结尾,不以 \\?\ 开头。
  35. /// </summary>
  36. public static IEnumerable<string> GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
  37. if (!Exists(path)) return [];
  38. Logger.Trace($"枚举文件夹({searchPattern},递归:{!topDirectoryOnly}):{path}");
  39. return Directory.EnumerateDirectories(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
  40. Select(PathUtils.WithoutLongPath);
  41. }
  42. /// <summary>
  43. /// 该文件夹是否为空。
  44. /// 如果文件夹不存在,也返回 true。
  45. /// </summary>
  46. public static bool IsEmpty(string path) {
  47. if (!Exists(path)) return true;
  48. return !Directory.EnumerateFileSystemEntries(PathUtils.WithLongPath(path)).Any();
  49. }
  50. /// <summary>
  51. /// 剪切文件夹。
  52. /// </summary>
  53. public static void Move(string sourceDirName, string destDirName) {
  54. if (!Exists(sourceDirName)) return;
  55. DirectoryUtils.Delete(destDirName); // Move 要求不存在对应文件夹
  56. Logger.Trace($"剪切文件夹:{sourceDirName} → {destDirName}");
  57. Directory.Move(PathUtils.WithLongPath(sourceDirName), PathUtils.WithLongPath(destDirName));
  58. }
  59. /// <summary>
  60. /// 删除文件夹。
  61. /// </summary>
  62. public static void Delete(string dirPath, bool toRecycleBin = false) {
  63. if (!Exists(dirPath)) return;
  64. Logger.Trace($"{(toRecycleBin ? "将文件夹删除到回收站" : "删除文件夹")}:{dirPath}");
  65. if (toRecycleBin) {
  66. FileUtils.DeleteToRecycleBin(dirPath);
  67. } else {
  68. Directory.Delete(PathUtils.WithLongPath(dirPath), recursive:true);
  69. }
  70. }
  71. }