182 lines
4.1 KiB
C#
182 lines
4.1 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace MAX.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}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建目录
|
|
/// </summary>
|
|
/// <param name="dir"></param>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取文件夹大小
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
}
|
|
}
|