DirTool.cs 7.4 KB

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