DirTool.cs 1.5 KB

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