FileTool.cs 7.4 KB

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