DirectoryUtils.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 = Path.GetDirectoryName(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. return Directory.Exists(PathUtils.WithLongPath(path));
  19. }
  20. /// <summary>
  21. /// 创建 <see cref="DirectoryInfo"/> 对象。
  22. /// </summary>
  23. public static DirectoryInfo GetInfo(string path) {
  24. return new(PathUtils.WithLongPath(path));
  25. }
  26. /// <summary>
  27. /// 返回指定路径下的所有文件,不以 \\?\ 开头。
  28. /// </summary>
  29. public static IEnumerable<string> GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
  30. if (!Exists(path)) return [];
  31. Logger.Trace($"枚举文件({searchPattern},递归:{!topDirectoryOnly}):{path}");
  32. return Directory.EnumerateFiles(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
  33. Select(PathUtils.WithoutLongPath);
  34. }
  35. /// <summary>
  36. /// 返回指定路径下的所有文件夹,不以分隔符结尾,不以 \\?\ 开头。
  37. /// </summary>
  38. public static IEnumerable<string> GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
  39. if (!Exists(path)) return [];
  40. Logger.Trace($"枚举文件夹({searchPattern},递归:{!topDirectoryOnly}):{path}");
  41. return Directory.EnumerateDirectories(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
  42. Select(PathUtils.WithoutLongPath);
  43. }
  44. /// <summary>
  45. /// 该文件夹是否为空。
  46. /// 如果文件夹不存在,也返回 true。
  47. /// </summary>
  48. public static bool IsEmpty(string path) {
  49. if (!Exists(path)) return true;
  50. return !Directory.EnumerateFileSystemEntries(PathUtils.WithLongPath(path)).Any();
  51. }
  52. /// <summary>
  53. /// 剪切文件夹。
  54. /// </summary>
  55. public static void Move(string sourceDirName, string destDirName) {
  56. if (!Exists(sourceDirName)) return;
  57. Create(destDirName);
  58. Logger.Trace($"剪切文件夹:{sourceDirName} → {destDirName}");
  59. Directory.Move(PathUtils.WithLongPath(sourceDirName), PathUtils.WithLongPath(destDirName));
  60. }
  61. /// <summary>
  62. /// 删除文件夹。
  63. /// </summary>
  64. public static void Delete(string dirPath, bool toRecycleBin = false) {
  65. if (!Exists(dirPath)) return;
  66. Logger.Trace($"{(toRecycleBin ? "将文件夹删除到回收站" : "删除文件夹")}:{dirPath}");
  67. if (toRecycleBin) {
  68. FileUtils.DeleteToRecycleBin(dirPath);
  69. } else {
  70. Directory.Delete(PathUtils.WithLongPath(dirPath));
  71. }
  72. }
  73. }