89 lines
2.6 KiB
C#
89 lines
2.6 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 文件操作帮助类
|
|
/// </summary>
|
|
public class FileHelper
|
|
{
|
|
/// <summary>
|
|
/// 获取文件MD5值
|
|
/// </summary>
|
|
/// <param name="FilePath"></param>
|
|
/// <returns></returns>
|
|
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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读取所有内容 相对路径Or绝对路径
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// 读取所有内容 绝对路径
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
public static string ReadAbsAllText(string file)
|
|
{
|
|
return _ReadAllText(file, Encoding.UTF8);
|
|
}
|
|
/// <summary>
|
|
/// 读取所有内容 相对路径
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
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}");
|
|
}
|
|
}
|
|
}
|
|
}
|