FileTool.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2017.3.29 - 2017.6.29
  4. // desc: 文件操作工具
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using Azylee.Core.DataUtils.CollectionUtils;
  8. using Azylee.Core.DataUtils.UnitConvertUtils;
  9. using Azylee.Core.IOUtils.DirUtils;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Security.Cryptography;
  15. namespace Azylee.Core.IOUtils.FileUtils
  16. {
  17. /// <summary>
  18. /// 文件操作工具
  19. /// </summary>
  20. public class FileTool
  21. {
  22. /// <summary>
  23. /// 判断字符串是文件路径
  24. /// </summary>
  25. /// <param name="s"></param>
  26. /// <returns></returns>
  27. public static bool IsFile(string s)
  28. {
  29. if (File.Exists(s)) return true;
  30. return false;
  31. }
  32. /// <summary>
  33. /// 获取文件(单层目录)
  34. /// </summary>
  35. /// <param name="path">路径</param>
  36. /// <param name="pattern">通配符</param>
  37. /// <returns></returns>
  38. public static List<string> GetFile(string path, string pattern = "*")
  39. {
  40. try
  41. {
  42. if (Directory.Exists(path))
  43. {
  44. List<string> result = Directory.EnumerateFiles(path, pattern).ToList();
  45. return result;
  46. }
  47. }
  48. catch (Exception e) { }
  49. return null;
  50. }
  51. ///// <summary>
  52. ///// 获取文件(向下钻取所有目录)
  53. ///// </summary>
  54. ///// <param name="path">路径</param>
  55. ///// <param name="pattern">通配符</param>
  56. ///// <returns></returns>
  57. //public static List<string> GetAllFile(string path, string pattern = "*")
  58. //{
  59. // List<string> result = null;
  60. // try
  61. // {
  62. // result = Directory.EnumerateFiles(path, pattern, SearchOption.TopDirectoryOnly).ToList();
  63. // }
  64. // catch (Exception e) { }
  65. // return result;
  66. //}
  67. /// <summary>
  68. /// 获取目录下的所有文件
  69. /// 防止遇到($文件夹报错无法获取目录的错误)
  70. /// </summary>
  71. /// <param name="path"></param>
  72. /// <param name="patterns"></param>
  73. /// <returns></returns>
  74. public static List<string> GetAllFile(string path, string[] patterns = null)
  75. {
  76. List<string> allpath = DirTool.GetAllPath(path);
  77. if (allpath == null) allpath = new List<string>();
  78. allpath.Add(path);
  79. return FileTool.GetAllFile(allpath, patterns);
  80. }
  81. /// <summary>
  82. /// 获取文件(多个目录)
  83. /// </summary>
  84. /// <param name="paths">路径(支持多个路径)</param>
  85. /// <param name="patterns">通配符(支持多个通配符)</param>
  86. /// <returns></returns>
  87. public static List<string> GetAllFile(List<string> paths, string[] patterns = null)
  88. {
  89. List<string> result = new List<string>();
  90. if (!ListTool.IsNullOrEmpty(paths))
  91. {
  92. foreach (var path in paths)
  93. {
  94. if (!ListTool.IsNullOrEmpty(patterns))
  95. {
  96. foreach (var pattern in patterns)
  97. {
  98. List<string> temp = GetFile(path, pattern);
  99. if (ListTool.HasElements(temp)) result.AddRange(temp);
  100. }
  101. }
  102. else
  103. {
  104. List<string> temp = GetFile(path);
  105. if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp);
  106. }
  107. }
  108. }
  109. return result;
  110. }
  111. ///// <summary>
  112. ///// 获取文件(所有目录)(严格模式:从第一个.开始截取后缀)
  113. ///// </summary>
  114. ///// <param name="paths">路径(支持多个路径)</param>
  115. ///// <param name="patterns">通配符(支持多个通配符)</param>
  116. ///// <returns></returns>
  117. //public static List<string> GetAllFileByExt(string[] paths, string[] patterns)
  118. //{
  119. // List<string> result = new List<string>();
  120. // if (!ListTool.IsNullOrEmpty(paths))
  121. // {
  122. // foreach (var path in paths)
  123. // {
  124. // List<string> temp = GetAllFile(path);
  125. // if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp);
  126. // }
  127. // }
  128. // if (!ListTool.IsNullOrEmpty(patterns) && !ListTool.IsNullOrEmpty(result))
  129. // {
  130. // for (int i = result.Count() - 1; i >= 0; i--)
  131. // {
  132. // string ext = System.IO.Path.GetFileName(result[i]);
  133. // if (ext.IndexOf('.') >= 0)
  134. // {
  135. // ext = ext.Substring(ext.IndexOf('.'));
  136. // }
  137. // if (!patterns.Contains(ext)) result.RemoveAt(i);
  138. // }
  139. // }
  140. // return result;
  141. //}
  142. /// <summary>
  143. /// 删除文件
  144. /// </summary>
  145. /// <param name="file">文件路径</param>
  146. /// <returns></returns>
  147. public static void Delete(string file)
  148. {
  149. try
  150. {
  151. File.Delete(file);
  152. }
  153. catch (Exception e) { }
  154. }
  155. /// <summary>
  156. /// 删除文件(多个)
  157. /// </summary>
  158. /// <param name="files">文件路径(支持多个文件路径)</param>
  159. /// <returns></returns>
  160. public static void Delete(string[] files)
  161. {
  162. if (ListTool.HasElements(files))
  163. {
  164. foreach (var file in files)
  165. {
  166. Delete(file);
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// 获取文件的大小(字节数)
  172. /// </summary>
  173. /// <param name="fileName"></param>
  174. /// <returns></returns>
  175. public static long Size(string fileName)
  176. {
  177. long result = -1;
  178. if (File.Exists(fileName))
  179. {
  180. try
  181. {
  182. FileInfo fi = new FileInfo(fileName);
  183. result = fi.Length;
  184. }
  185. catch (Exception e) { }
  186. }
  187. return result;
  188. }
  189. /// <summary>
  190. /// 获取多个文件的大小(字节数)
  191. /// </summary>
  192. /// <param name="files"></param>
  193. /// <returns></returns>
  194. public static long[] Size(List<string> files)
  195. {
  196. long[] result = new long[files.Count];
  197. for (int i = 0; i < result.Length; i++)
  198. {
  199. result[i] = Size(files[i]);
  200. }
  201. return result;
  202. }
  203. /// <summary>
  204. /// 获取文件大小(根据单位换算)
  205. /// </summary>
  206. /// <param name="fileName"></param>
  207. /// <param name="unit">B,KB,MB,GB</param>
  208. /// <returns></returns>
  209. public static double Size(string fileName, string unit)
  210. {
  211. return ByteConvertTool.Cvt(Size(fileName), unit);
  212. }
  213. /// <summary>
  214. /// 获取文件大小信息(自动适配)(如:1MB,10KB...)
  215. /// </summary>
  216. /// <param name="fileName"></param>
  217. /// <returns></returns>
  218. public static string SizeFormat(string fileName)
  219. {
  220. return ByteConvertTool.Fmt(Size(fileName));
  221. }
  222. /// <summary>
  223. /// 获取文件的MD5特征码
  224. /// </summary>
  225. /// <param name="file"></param>
  226. /// <returns></returns>
  227. public static string GetMD5(string file)
  228. {
  229. string result = string.Empty;
  230. if (!File.Exists(file)) return result;
  231. using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
  232. {
  233. HashAlgorithm algorithm = MD5.Create();
  234. byte[] hashBytes = algorithm.ComputeHash(fs);
  235. result = BitConverter.ToString(hashBytes).Replace("-", "");
  236. }
  237. return result;
  238. }
  239. /// <summary>
  240. /// 获取多个文件的MD5特征码
  241. /// </summary>
  242. /// <param name="file"></param>
  243. /// <returns></returns>
  244. public static string[] GetMD5(List<string> files)
  245. {
  246. string[] result = new string[files.Count];
  247. for (int i = 0; i < files.Count; i++)
  248. {
  249. result[i] = GetMD5(files[i]);
  250. }
  251. return result;
  252. }
  253. public static bool Copy(string sourceFileName, string destFileName, bool overwrite)
  254. {
  255. if (File.Exists(sourceFileName))
  256. {
  257. string destPath = DirTool.GetFilePath(destFileName);
  258. if (DirTool.Create(destPath))
  259. {
  260. try
  261. {
  262. File.Copy(sourceFileName, destFileName, overwrite);
  263. return true;
  264. }
  265. catch { }
  266. }
  267. }
  268. return false;
  269. }
  270. }
  271. }