FileTool.cs 7.2 KB

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