mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-05-25 10:05:49 +08:00
113 lines
3.4 KiB
C#
113 lines
3.4 KiB
C#
using System.IO;
|
|
|
|
namespace BetterGenshinImpact.Helpers;
|
|
|
|
public class DirectoryHelper
|
|
{
|
|
public static void DeleteReadOnlyDirectory(string directoryPath)
|
|
{
|
|
if (Directory.Exists(directoryPath))
|
|
{
|
|
// 获取目录信息
|
|
var directoryInfo = new DirectoryInfo(directoryPath);
|
|
|
|
// 移除目录及其内容的只读属性
|
|
RemoveReadOnlyAttribute(directoryInfo);
|
|
|
|
// 删除目录
|
|
Directory.Delete(directoryPath, true);
|
|
}
|
|
}
|
|
|
|
public static void DeleteDirectoryWithReadOnlyCheck(string directoryPath)
|
|
{
|
|
if (Directory.Exists(directoryPath))
|
|
{
|
|
// 获取目录信息
|
|
var directoryInfo = new DirectoryInfo(directoryPath);
|
|
|
|
// 递归删除目录及其内容
|
|
DeleteDirectory(directoryInfo);
|
|
}
|
|
}
|
|
|
|
private static void DeleteDirectory(DirectoryInfo directoryInfo)
|
|
{
|
|
|
|
//通过软链接生成的目录,直接删除该链接目录,而不涉及其文件本体
|
|
var attributes = directoryInfo.Attributes;
|
|
if ((attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
|
|
{
|
|
directoryInfo.Delete();
|
|
return;
|
|
}
|
|
|
|
// 递归处理子目录
|
|
foreach (var subDirectory in directoryInfo.GetDirectories())
|
|
{
|
|
DeleteDirectory(subDirectory);
|
|
}
|
|
|
|
// 移除文件的只读属性并删除文件
|
|
foreach (var file in directoryInfo.GetFiles())
|
|
{
|
|
if (file.Attributes.HasFlag(FileAttributes.ReadOnly))
|
|
{
|
|
file.Attributes &= ~FileAttributes.ReadOnly;
|
|
}
|
|
file.Delete();
|
|
}
|
|
|
|
// 移除目录的只读属性并删除目录
|
|
if (directoryInfo.Attributes.HasFlag(FileAttributes.ReadOnly))
|
|
{
|
|
directoryInfo.Attributes &= ~FileAttributes.ReadOnly;
|
|
}
|
|
directoryInfo.Delete();
|
|
}
|
|
|
|
private static void RemoveReadOnlyAttribute(DirectoryInfo directoryInfo)
|
|
{
|
|
// 移除目录的只读属性
|
|
if (directoryInfo.Attributes.HasFlag(FileAttributes.ReadOnly))
|
|
{
|
|
directoryInfo.Attributes &= ~FileAttributes.ReadOnly;
|
|
}
|
|
|
|
// 移除文件的只读属性
|
|
foreach (var file in directoryInfo.GetFiles())
|
|
{
|
|
if (file.Attributes.HasFlag(FileAttributes.ReadOnly))
|
|
{
|
|
file.Attributes &= ~FileAttributes.ReadOnly;
|
|
}
|
|
}
|
|
|
|
// 递归处理子目录
|
|
foreach (var subDirectory in directoryInfo.GetDirectories())
|
|
{
|
|
RemoveReadOnlyAttribute(subDirectory);
|
|
}
|
|
}
|
|
|
|
public static void CopyDirectory(string sourceDir, string destDir)
|
|
{
|
|
// 创建目标目录
|
|
Directory.CreateDirectory(destDir);
|
|
|
|
// 获取源目录中的所有文件
|
|
foreach (var file in Directory.GetFiles(sourceDir))
|
|
{
|
|
var destFile = Path.Combine(destDir, Path.GetFileName(file));
|
|
File.Copy(file, destFile, true); // 覆盖同名文件
|
|
}
|
|
|
|
// 获取源目录中的所有子目录
|
|
foreach (var subDir in Directory.GetDirectories(sourceDir))
|
|
{
|
|
var destSubDir = Path.Combine(destDir, Path.GetFileName(subDir));
|
|
CopyDirectory(subDir, destSubDir); // 递归拷贝子目录
|
|
}
|
|
}
|
|
}
|