FileTool.cs 6.2 KB

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