FileTool.cs 6.8 KB

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