//************************************************************************ // https://github.com/yuzhengyang // author: yuzhengyang // date: 2017.3.29 - 2017.6.10 // desc: 文件操作工具 // Copyright (c) yuzhengyang. All rights reserved. //************************************************************************ using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using Y.Utils.DataUtils.Collections; using Y.Utils.DataUtils.UnitConvertUtils; namespace Y.Utils.IOUtils.FileUtils { /// /// 文件操作工具 /// public class FileTool { /// /// 获取文件(单层目录) /// /// 路径 /// 通配符 /// public static List GetFile(string path, string pattern = "*") { if (Directory.Exists(path)) try { List result = Directory.EnumerateFiles(path, pattern).ToList(); return result; } catch (Exception e) { } return null; } /// /// 获取文件(所有目录) /// /// 路径 /// 通配符 /// public static List GetAllFile(string path, string pattern = "*") { List result = null; try { result = Directory.EnumerateFiles(path, pattern, SearchOption.AllDirectories).ToList(); } catch (Exception e) { } return result; } /// /// 获取文件(所有目录) /// /// 路径 /// 通配符(支持多个通配符) /// public static List GetAllFile(string path, string[] pattern) { List result = new List(); if (!ListTool.IsNullOrEmpty(pattern)) { foreach (var p in pattern) { List temp = GetAllFile(path, p); if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp); } } return result; } /// /// 获取文件(所有目录) /// /// 路径(支持多个路径) /// 通配符(支持多个通配符) /// public static List GetAllFile(string[] paths, string[] patterns) { List result = new List(); if (!ListTool.IsNullOrEmpty(paths)) { foreach (var path in paths) { if (!ListTool.IsNullOrEmpty(patterns)) { foreach (var pattern in patterns) { List temp = GetAllFile(path, pattern); if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp); } } else { List temp = GetAllFile(path); if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp); } } } return result; } /// /// 获取文件(所有目录)(严格模式:从第一个.开始截取后缀) /// /// 路径(支持多个路径) /// 通配符(支持多个通配符) /// public static List GetAllFileByExt(string[] paths, string[] patterns) { List result = new List(); if (!ListTool.IsNullOrEmpty(paths)) { foreach (var path in paths) { List temp = GetAllFile(path); if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp); } } if (!ListTool.IsNullOrEmpty(patterns) && !ListTool.IsNullOrEmpty(result)) { for (int i = result.Count() - 1; i >= 0; i--) { string ext = System.IO.Path.GetFileName(result[i]); if (ext.IndexOf('.') >= 0) { ext = ext.Substring(ext.IndexOf('.')); } if (!patterns.Contains(ext)) result.RemoveAt(i); } } return result; } /// /// 删除文件 /// /// 文件路径 /// public static void Delete(string file) { try { File.Delete(file); } catch (Exception e) { } } /// /// 删除文件(多个) /// /// 文件路径(支持多个文件路径) /// public static void Delete(string[] files) { if (ListTool.HasElements(files)) { foreach (var file in files) { Delete(file); } } } /// /// 获取文件的大小(字节数) /// /// /// public static long Size(string fileName) { long result = -1; if (File.Exists(fileName)) { try { FileInfo fi = new FileInfo(fileName); result = fi.Length; } catch (Exception e) { } } return result; } /// /// 获取多个文件的大小(字节数) /// /// /// public static long[] Size(List files) { long[] result = new long[files.Count]; for (int i = 0; i < result.Length; i++) { result[i] = Size(files[i]); } return result; } /// /// 获取文件大小(根据单位换算) /// /// /// B,KB,MB,GB /// public static double Size(string fileName, string unit) { return ByteConvertTool.Cvt(Size(fileName), unit); } /// /// 获取文件大小信息(自动适配)(如:1MB,10KB...) /// /// /// public static string SizeFormat(string fileName) { return ByteConvertTool.Fmt(Size(fileName)); } /// /// 获取文件的MD5特征码 /// /// /// public static string GetMD5(string file) { string result = string.Empty; if (!File.Exists(file)) return result; using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) { HashAlgorithm algorithm = MD5.Create(); byte[] hashBytes = algorithm.ComputeHash(fs); result = BitConverter.ToString(hashBytes).Replace("-", ""); } return result; } /// /// 获取多个文件的MD5特征码 /// /// /// public static string[] GetMD5(List files) { string[] result = new string[files.Count]; for (int i = 0; i < files.Count; i++) { result[i] = GetMD5(files[i]); } return result; } } }