FileTool.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.3.29 - 2017.6.29
  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.Security.Cryptography;
  13. using Y.Utils.DataUtils.Collections;
  14. using Y.Utils.DataUtils.UnitConvertUtils;
  15. using Y.Utils.IOUtils.PathUtils;
  16. namespace Y.Utils.IOUtils.FileUtils
  17. {
  18. /// <summary>
  19. /// 文件操作工具
  20. /// </summary>
  21. public class FileTool
  22. {
  23. /// <summary>
  24. /// 判断字符串是文件路径
  25. /// </summary>
  26. /// <param name="s"></param>
  27. /// <returns></returns>
  28. public static bool IsFile(string s)
  29. {
  30. if (File.Exists(s)) return true;
  31. return false;
  32. }
  33. /// <summary>
  34. /// 获取文件(单层目录)
  35. /// </summary>
  36. /// <param name="path">路径</param>
  37. /// <param name="pattern">通配符</param>
  38. /// <returns></returns>
  39. public static List<string> GetFile(string path, string pattern = "*")
  40. {
  41. if (Directory.Exists(path))
  42. try
  43. {
  44. List<string> result = Directory.EnumerateFiles(path, pattern).ToList();
  45. return result;
  46. }
  47. catch (Exception e) { }
  48. return null;
  49. }
  50. ///// <summary>
  51. ///// 获取文件(向下钻取所有目录)
  52. ///// </summary>
  53. ///// <param name="path">路径</param>
  54. ///// <param name="pattern">通配符</param>
  55. ///// <returns></returns>
  56. //public static List<string> GetAllFile(string path, string pattern = "*")
  57. //{
  58. // List<string> result = null;
  59. // try
  60. // {
  61. // result = Directory.EnumerateFiles(path, pattern, SearchOption.TopDirectoryOnly).ToList();
  62. // }
  63. // catch (Exception e) { }
  64. // return result;
  65. //}
  66. /// <summary>
  67. /// 获取目录下的所有文件
  68. /// 防止遇到($文件夹报错无法获取目录的错误)
  69. /// </summary>
  70. /// <param name="path"></param>
  71. /// <param name="patterns"></param>
  72. /// <returns></returns>
  73. public static List<string> GetAllFile(string path, string[] patterns = null)
  74. {
  75. List<string> allpath = DirTool.GetAllPath(path);
  76. if (allpath == null) allpath = new List<string>();
  77. allpath.Add(path);
  78. return FileTool.GetAllFile(allpath, patterns);
  79. }
  80. /// <summary>
  81. /// 获取文件(多个目录)(向下钻取所有目录)
  82. /// </summary>
  83. /// <param name="paths">路径(支持多个路径)</param>
  84. /// <param name="patterns">通配符(支持多个通配符)</param>
  85. /// <returns></returns>
  86. public static List<string> GetAllFile(List<string> paths, string[] patterns = null)
  87. {
  88. List<string> result = new List<string>();
  89. if (!ListTool.IsNullOrEmpty(paths))
  90. {
  91. foreach (var path in paths)
  92. {
  93. if (!ListTool.IsNullOrEmpty(patterns))
  94. {
  95. foreach (var pattern in patterns)
  96. {
  97. List<string> temp = GetFile(path, pattern);
  98. if (ListTool.HasElements(temp)) result.AddRange(temp);
  99. }
  100. }
  101. else
  102. {
  103. List<string> temp = GetFile(path);
  104. if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp);
  105. }
  106. }
  107. }
  108. return result;
  109. }
  110. ///// <summary>
  111. ///// 获取文件(所有目录)(严格模式:从第一个.开始截取后缀)
  112. ///// </summary>
  113. ///// <param name="paths">路径(支持多个路径)</param>
  114. ///// <param name="patterns">通配符(支持多个通配符)</param>
  115. ///// <returns></returns>
  116. //public static List<string> GetAllFileByExt(string[] paths, string[] patterns)
  117. //{
  118. // List<string> result = new List<string>();
  119. // if (!ListTool.IsNullOrEmpty(paths))
  120. // {
  121. // foreach (var path in paths)
  122. // {
  123. // List<string> temp = GetAllFile(path);
  124. // if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp);
  125. // }
  126. // }
  127. // if (!ListTool.IsNullOrEmpty(patterns) && !ListTool.IsNullOrEmpty(result))
  128. // {
  129. // for (int i = result.Count() - 1; i >= 0; i--)
  130. // {
  131. // string ext = System.IO.Path.GetFileName(result[i]);
  132. // if (ext.IndexOf('.') >= 0)
  133. // {
  134. // ext = ext.Substring(ext.IndexOf('.'));
  135. // }
  136. // if (!patterns.Contains(ext)) result.RemoveAt(i);
  137. // }
  138. // }
  139. // return result;
  140. //}
  141. /// <summary>
  142. /// 删除文件
  143. /// </summary>
  144. /// <param name="file">文件路径</param>
  145. /// <returns></returns>
  146. public static void Delete(string file)
  147. {
  148. try
  149. {
  150. File.Delete(file);
  151. }
  152. catch (Exception e) { }
  153. }
  154. /// <summary>
  155. /// 删除文件(多个)
  156. /// </summary>
  157. /// <param name="files">文件路径(支持多个文件路径)</param>
  158. /// <returns></returns>
  159. public static void Delete(string[] files)
  160. {
  161. if (ListTool.HasElements(files))
  162. {
  163. foreach (var file in files)
  164. {
  165. Delete(file);
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// 获取文件的大小(字节数)
  171. /// </summary>
  172. /// <param name="fileName"></param>
  173. /// <returns></returns>
  174. public static long Size(string fileName)
  175. {
  176. long result = -1;
  177. if (File.Exists(fileName))
  178. {
  179. try
  180. {
  181. FileInfo fi = new FileInfo(fileName);
  182. result = fi.Length;
  183. }
  184. catch (Exception e) { }
  185. }
  186. return result;
  187. }
  188. /// <summary>
  189. /// 获取多个文件的大小(字节数)
  190. /// </summary>
  191. /// <param name="files"></param>
  192. /// <returns></returns>
  193. public static long[] Size(List<string> files)
  194. {
  195. long[] result = new long[files.Count];
  196. for (int i = 0; i < result.Length; i++)
  197. {
  198. result[i] = Size(files[i]);
  199. }
  200. return result;
  201. }
  202. /// <summary>
  203. /// 获取文件大小(根据单位换算)
  204. /// </summary>
  205. /// <param name="fileName"></param>
  206. /// <param name="unit">B,KB,MB,GB</param>
  207. /// <returns></returns>
  208. public static double Size(string fileName, string unit)
  209. {
  210. return ByteConvertTool.Cvt(Size(fileName), unit);
  211. }
  212. /// <summary>
  213. /// 获取文件大小信息(自动适配)(如:1MB,10KB...)
  214. /// </summary>
  215. /// <param name="fileName"></param>
  216. /// <returns></returns>
  217. public static string SizeFormat(string fileName)
  218. {
  219. return ByteConvertTool.Fmt(Size(fileName));
  220. }
  221. /// <summary>
  222. /// 获取文件的MD5特征码
  223. /// </summary>
  224. /// <param name="file"></param>
  225. /// <returns></returns>
  226. public static string GetMD5(string file)
  227. {
  228. string result = string.Empty;
  229. if (!File.Exists(file)) return result;
  230. using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
  231. {
  232. HashAlgorithm algorithm = MD5.Create();
  233. byte[] hashBytes = algorithm.ComputeHash(fs);
  234. result = BitConverter.ToString(hashBytes).Replace("-", "");
  235. }
  236. return result;
  237. }
  238. /// <summary>
  239. /// 获取多个文件的MD5特征码
  240. /// </summary>
  241. /// <param name="file"></param>
  242. /// <returns></returns>
  243. public static string[] GetMD5(List<string> files)
  244. {
  245. string[] result = new string[files.Count];
  246. for (int i = 0; i < files.Count; i++)
  247. {
  248. result[i] = GetMD5(files[i]);
  249. }
  250. return result;
  251. }
  252. }
  253. }