DirTool.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using Y.Utils.DataUtils.Collections;
  10. namespace Y.Utils.IOUtils.PathUtils
  11. {
  12. public class DirTool
  13. {
  14. public static bool Create(string path)
  15. {
  16. if (Directory.Exists(path))
  17. return true;
  18. else
  19. try { Directory.CreateDirectory(path); return true; } catch (Exception e) { }
  20. return false;
  21. }
  22. public static string Parent(string path)
  23. {
  24. try
  25. {
  26. return Directory.GetParent(path).ToString();
  27. }
  28. catch (Exception e) { }
  29. return null;
  30. }
  31. public static List<string> GetPath(string path)
  32. {
  33. if (Directory.Exists(path))
  34. try { return Directory.EnumerateDirectories(path).ToList(); } catch (Exception e) { }
  35. return null;
  36. }
  37. public static List<string> GetAllPath(string path)
  38. {
  39. List<string> result = GetPath(path);
  40. if (!ListTool.IsNullOrEmpty(result))
  41. {
  42. List<string> temp = new List<string>();
  43. foreach (var item in result)
  44. {
  45. List<string> t = GetAllPath(item);
  46. if (!ListTool.IsNullOrEmpty(t))
  47. temp.AddRange(t);
  48. }
  49. result.AddRange(temp);
  50. return result;
  51. }
  52. return null;
  53. }
  54. }
  55. }