DirTool.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 System.Text;
  10. using Y.Utils.DataUtils.Collections;
  11. namespace Y.Utils.IOUtils.PathUtils
  12. {
  13. public class DirTool
  14. {
  15. public static bool Create(string path)
  16. {
  17. if (Directory.Exists(path))
  18. return true;
  19. else
  20. try
  21. {
  22. Directory.CreateDirectory(path); return true;
  23. }
  24. catch (Exception e)
  25. {
  26. }
  27. return false;
  28. }
  29. public static string Parent(string path)
  30. {
  31. try
  32. {
  33. return Directory.GetParent(path).ToString();
  34. }
  35. catch (Exception e) { }
  36. return null;
  37. }
  38. public static List<string> GetPath(string path)
  39. {
  40. if (Directory.Exists(path))
  41. try { return Directory.EnumerateDirectories(path).ToList(); } catch (Exception e) { }
  42. return null;
  43. }
  44. public static List<string> GetAllPath(string path)
  45. {
  46. List<string> result = GetPath(path);
  47. if (!ListTool.IsNullOrEmpty(result))
  48. {
  49. List<string> temp = new List<string>();
  50. foreach (var item in result)
  51. {
  52. List<string> t = GetAllPath(item);
  53. if (!ListTool.IsNullOrEmpty(t))
  54. temp.AddRange(t);
  55. }
  56. result.AddRange(temp);
  57. return result;
  58. }
  59. return null;
  60. }
  61. public static bool IsDriver(string path)
  62. {
  63. if (path != null && path.Length >= 2)
  64. {
  65. if (path.Substring(1, 1) == ":")
  66. {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. /// <summary>
  73. /// 获取文件所在的目录
  74. /// </summary>
  75. /// <param name="filePath"></param>
  76. /// <returns></returns>
  77. public static string GetFilePath(string filePath)
  78. {
  79. string result = "";
  80. if (!string.IsNullOrWhiteSpace(filePath))
  81. {
  82. string fileName = Path.GetFileName(filePath);
  83. result = filePath.Substring(0, filePath.Length - fileName.Length);
  84. }
  85. return result;
  86. }
  87. public static string Combine(params string[] paths)
  88. {
  89. if (ListTool.HasElements(paths))
  90. {
  91. if (paths.Length > 1)
  92. {
  93. StringBuilder result = new StringBuilder(paths[0]);
  94. for (int i = 1; i < paths.Length; i++)
  95. {
  96. result.Append("\\");
  97. result.Append(paths[i]);
  98. }
  99. while (result.ToString().IndexOf("\\\\") >= 0)
  100. {
  101. result.Replace("\\\\", "\\");
  102. }
  103. return result.ToString();
  104. }
  105. else
  106. {
  107. return paths[0];
  108. }
  109. }
  110. return "";
  111. }
  112. }
  113. }