DirTool.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using Y.Utils.Net20.ListUtils;
  6. namespace Y.Utils.Net20.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. List<string> result = null;
  30. if (Directory.Exists(path))
  31. {
  32. try
  33. {
  34. string[] list = Directory.GetDirectories(path);
  35. if (ListTool.HasElements(list))
  36. result = new List<string>(list);
  37. }
  38. catch (Exception e) { }
  39. }
  40. return result;
  41. }
  42. public static List<string> GetAllPath(string path)
  43. {
  44. List<string> result = GetPath(path);
  45. if (!ListTool.IsNullOrEmpty(result))
  46. {
  47. List<string> temp = new List<string>();
  48. foreach (var item in result)
  49. {
  50. List<string> t = GetAllPath(item);
  51. if (!ListTool.IsNullOrEmpty(t))
  52. temp.AddRange(t);
  53. }
  54. result.AddRange(temp);
  55. return result;
  56. }
  57. return null;
  58. }
  59. }
  60. }