FileTool.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using Y.Utils.Net20.ListUtils;
  6. namespace Y.Utils.Net20.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. List<string> result = null;
  19. if (Directory.Exists(path))
  20. {
  21. try
  22. {
  23. string[] list = Directory.GetFiles(path, pattern);
  24. if (ListTool.HasElements(list))
  25. result = new List<string>(list);
  26. }
  27. catch (Exception e) { }
  28. }
  29. return result;
  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. string[] list = Directory.GetFiles(path, pattern, SearchOption.AllDirectories);
  43. if (ListTool.HasElements(list))
  44. result = new List<string>(list);
  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(List<string> paths, List<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 = 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 (File.Exists(file))
  139. {
  140. 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 SizeFormat(Size(fileName));
  176. }
  177. public static string SizeFormat(long size)
  178. {
  179. string rs = "";
  180. if (size > 1024 * 1024 * 1024)
  181. {
  182. rs = Math.Round((double)size / 1024 / 1024 / 1024, 2) + " GB";
  183. }
  184. else if (size > 1024 * 1024)
  185. {
  186. rs = Math.Round((double)size / 1024 / 1024, 2) + " MB";
  187. }
  188. else if (size > 1024)
  189. {
  190. rs = Math.Round((double)size / 1024, 2) + " KB";
  191. }
  192. else
  193. {
  194. rs = size + " B";
  195. }
  196. return rs;
  197. }
  198. public static string SizeConvert(string fileName, string unit)
  199. {
  200. return SizeConvert(Size(fileName), unit);
  201. }
  202. public static string SizeConvert(long size, string unit)
  203. {
  204. double rs = 0;
  205. switch (unit)
  206. {
  207. case "B": rs = (double)size; break;
  208. case "KB": rs = (double)size / 1024; break;
  209. case "MB": rs = (double)size / 1024 / 1024; break;
  210. case "GB": rs = (double)size / 1024 / 1024 / 1024; break;
  211. }
  212. return Math.Round(rs, 2).ToString();
  213. }
  214. }
  215. }