using Store.Framework.Configs; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Store.Framework.Helpers { /// /// 文件操作帮助类 /// public class FileHelper { /// /// 获取文件MD5值 /// /// /// public static string GetFileMd5(string FilePath) { try { FileStream get_file = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read); System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] hash_byte = get_md5.ComputeHash(get_file); string resule = BitConverter.ToString(hash_byte); resule = resule.Replace("-", ""); get_file.Close(); return resule; } catch (Exception e) { return e.ToString(); } } /// /// 读取所有内容 相对路径Or绝对路径 /// /// /// public static string ReadAllText(string file) { if (!Path.IsPathRooted(file)) { file = Path.Combine(GlobalConfig.AppDirectory, file); } if (File.Exists(file)) { return _ReadAllText(file, Encoding.UTF8); } return string.Empty; } /// /// 读取所有内容 绝对路径 /// /// /// public static string ReadAbsAllText(string file) { return _ReadAllText(file, Encoding.UTF8); } /// /// 读取所有内容 相对路径 /// /// /// public static string ReadPartialAllText(string file) { string _file = Path.Combine(GlobalConfig.AppDirectory, file); return _ReadAllText(_file, Encoding.UTF8); } private static string _ReadAllText(string file, Encoding encoding) { if (File.Exists(file)) { return File.ReadAllText(file, encoding); } else { throw new Exception($"缺失文件{file}"); } } } }