DirTool.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.3.29 - 2017.6.27
  5. // desc: 文件目录工具类
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using Y.Utils.DataUtils.Collections;
  14. namespace Y.Utils.IOUtils.PathUtils
  15. {
  16. /// <summary>
  17. /// 文件目录工具类
  18. /// </summary>
  19. public class DirTool
  20. {
  21. /// <summary>
  22. /// 创建文件目录(文件不存在则创建)
  23. /// </summary>
  24. /// <param name="path"></param>
  25. /// <returns>
  26. /// 如果文件已存在,返回true
  27. /// 如果文件不存在,则创建文件,成功返回true,失败返回false
  28. /// </returns>
  29. public static bool Create(string path)
  30. {
  31. if (Directory.Exists(path))
  32. return true;
  33. else
  34. try
  35. {
  36. Directory.CreateDirectory(path); return true;
  37. }
  38. catch (Exception e)
  39. {
  40. }
  41. return false;
  42. }
  43. /// <summary>
  44. /// 获取目录的父目录
  45. /// </summary>
  46. /// <param name="path"></param>
  47. /// <returns></returns>
  48. public static string Parent(string path)
  49. {
  50. try
  51. {
  52. return Directory.GetParent(path).ToString();
  53. }
  54. catch (Exception e) { }
  55. return null;
  56. }
  57. /// <summary>
  58. /// 获取目录下的目录(一层)
  59. /// </summary>
  60. /// <param name="path"></param>
  61. /// <returns></returns>
  62. public static List<string> GetPath(string path)
  63. {
  64. if (Directory.Exists(path))
  65. try { return Directory.EnumerateDirectories(path).ToList(); } catch (Exception e) { }
  66. return null;
  67. }
  68. /// <summary>
  69. /// 获取目录下所有目录(递归)
  70. /// </summary>
  71. /// <param name="path"></param>
  72. /// <returns></returns>
  73. public static List<string> GetAllPath(string path)
  74. {
  75. List<string> result = GetPath(path);
  76. if (!ListTool.IsNullOrEmpty(result))
  77. {
  78. List<string> temp = new List<string>();
  79. foreach (var item in result)
  80. {
  81. List<string> t = GetAllPath(item);
  82. if (!ListTool.IsNullOrEmpty(t))
  83. temp.AddRange(t);
  84. }
  85. result.AddRange(temp);
  86. return result;
  87. }
  88. return null;
  89. }
  90. /// <summary>
  91. /// 判断目录是否为磁盘
  92. /// </summary>
  93. /// <param name="path"></param>
  94. /// <returns></returns>
  95. public static bool IsDriver(string path)
  96. {
  97. if (path != null && path.Length >= 2)
  98. {
  99. if (path.Substring(1, 1) == ":")
  100. {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. /// <summary>
  107. /// 获取文件所在的目录
  108. /// </summary>
  109. /// <param name="filePath"></param>
  110. /// <returns></returns>
  111. public static string GetFilePath(string filePath)
  112. {
  113. string result = "";
  114. if (!string.IsNullOrWhiteSpace(filePath))
  115. {
  116. string fileName = Path.GetFileName(filePath);
  117. result = filePath.Substring(0, filePath.Length - fileName.Length);
  118. }
  119. return result;
  120. }
  121. /// <summary>
  122. /// 连接多个string构成目录
  123. /// </summary>
  124. /// <param name="paths"></param>
  125. /// <returns></returns>
  126. public static string Combine(params string[] paths)
  127. {
  128. if (ListTool.HasElements(paths))
  129. {
  130. if (paths.Length > 1)
  131. {
  132. StringBuilder result = new StringBuilder(paths[0]);
  133. for (int i = 1; i < paths.Length; i++)
  134. {
  135. result.Append("\\");
  136. result.Append(paths[i]);
  137. }
  138. while (result.ToString().IndexOf("\\\\") >= 0)
  139. {
  140. result.Replace("\\\\", "\\");
  141. }
  142. return result.ToString();
  143. }
  144. else
  145. {
  146. return paths[0];
  147. }
  148. }
  149. return "";
  150. }
  151. /// <summary>
  152. /// 路径包含关系
  153. /// </summary>
  154. /// <param name="path1"></param>
  155. /// <param name="path2"></param>
  156. /// <returns>
  157. /// -1:不存在包含关系
  158. /// 0:两个目录相同
  159. /// 1:path1 包含 path2(path1 大)
  160. /// 2:path2 包含 path1(path2 大)
  161. /// </returns>
  162. public static int Include(string path1, string path2)
  163. {
  164. if (path1 == path2) return 0;//两个目录相同
  165. string p1 = Combine(path1 + "\\");
  166. string p2 = Combine(path2 + "\\");
  167. if (p1 == p2) return 0;//两个目录相同(防止路径后有带\或不带\的情况)
  168. if (p1.Length > p2.Length && p1.Contains(p2)) return 1;//path1 包含 path2(path1 大)
  169. if (p2.Length > p1.Length && p2.Contains(p1)) return 2;//path2 包含 path1(path2 大)
  170. return -1;//不存在包含关系
  171. }
  172. }
  173. }