//************************************************************************ // author: yuzhengyang // date: 2017.3.29 - 2017.6.29 // desc: 文件操作工具 // Copyright (c) yuzhengyang. All rights reserved. //************************************************************************ using Azylee.Core.DataUtils.CollectionUtils; using Azylee.Core.DataUtils.UnitConvertUtils; using Azylee.Core.IOUtils.DirUtils; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; namespace Azylee.Core.IOUtils.FileUtils { /// /// 文件操作工具 /// public class FileTool { /// /// 判断字符串是文件路径 /// /// /// public static bool IsFile(string s) { if (File.Exists(s)) return true; return false; } /// /// 获取文件(单层目录) /// /// 路径 /// 通配符 /// public static List GetFile(string path, string pattern = "*") { try { if (Directory.Exists(path)) { 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.TopDirectoryOnly).ToList(); // } // catch (Exception e) { } // return result; //} /// /// 获取目录下的所有文件 /// 防止遇到($文件夹报错无法获取目录的错误) /// /// /// /// public static List GetAllFile(string path, string[] patterns = null) { List allpath = DirTool.GetAllPath(path); if (allpath == null) allpath = new List(); allpath.Add(path); return FileTool.GetAllFile(allpath, patterns); } /// /// 获取文件(多个目录)(向下钻取所有目录) /// /// 路径(支持多个路径) /// 通配符(支持多个通配符) /// public static List GetAllFile(List paths, string[] patterns = null) { List result = new List(); if (!ListTool.IsNullOrEmpty(paths)) { foreach (var path in paths) { if (!ListTool.IsNullOrEmpty(patterns)) { foreach (var pattern in patterns) { List temp = GetFile(path, pattern); if (ListTool.HasElements(temp)) result.AddRange(temp); } } else { List temp = GetFile(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; } public static bool Copy(string sourceFileName, string destFileName, bool overwrite) { if (File.Exists(sourceFileName)) { string destPath = DirTool.GetFilePath(destFileName); if (DirTool.Create(destPath)) { try { File.Copy(sourceFileName, destFileName, overwrite); return true; } catch { } } } return false; } } }