FileTool.cs 7.0 KB

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