using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace MAX.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}");
}
}
///
/// 创建目录
///
///
public static void CreateDirectory(string dir)
{
if (Path.HasExtension(dir))
{
dir = Path.GetDirectoryName(dir);
}
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
}
public static bool DeleteDirectory(string path)
{
try
{
Directory.Delete(path, true);
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return false;
}
}
public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive = true)
{
// Get information about the source directory
DirectoryInfo dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists
if (!dir.Exists)
{
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
}
// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();
// Create the destination directory
Directory.CreateDirectory(destinationDir);
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath);
}
// If recursive and copying subdirectories, recursively call this method
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
}
///
/// 获取文件夹大小
///
///
///
public static long GetDirectorySize(string path)
{
long result = 0;
try
{
if (Directory.Exists(path))
{
dynamic fso = Activator.CreateInstance(Type.GetTypeFromProgID("Scripting.FileSystemObject"));
dynamic fldr = fso.GetFolder(path);
result = (long)fldr.size;
}
else
{
result = 0;
}
}
catch (Exception ex)
{
result = 0;
}
return result;
}
}
}