DirTool.cs 6.7 KB

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