feat: 在生成目录结构时添加文件复制功能
在生成目录结构的 json 表示时,新增了将文件复制到指定目录并以 MD5 值命名的功能。同时,创建了用于存储 MD5 值对应文件的目录,并在 UI 中显示复制过程的状态。
This commit is contained in:
parent
eaa9328bfe
commit
d7fd841906
38
Form1.cs
38
Form1.cs
@ -26,6 +26,7 @@ namespace MD5Create
|
||||
progressBar1.Step = 1;
|
||||
}
|
||||
|
||||
// 程序启动前检查目录和版本号
|
||||
private void Start_Button_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Path1_Box.Text))
|
||||
@ -90,16 +91,36 @@ namespace MD5Create
|
||||
}
|
||||
|
||||
// 生成目录结构的 json 表示
|
||||
private Dictionary<string, object> GenerateDirectoryJson(string directoryPath, string rootPath,
|
||||
int totalFiles, ref int processedFiles, string excludeFolder)
|
||||
private Dictionary<string, object> GenerateDirectoryJson(string directoryPath, string rootPath, int totalFiles, ref int processedFiles, string excludeFolder, string fileFolder)
|
||||
{
|
||||
var result = new Dictionary<string, object>();
|
||||
|
||||
// 处理当前目录文件
|
||||
foreach (var file in Directory.GetFiles(directoryPath))
|
||||
{
|
||||
// 计算文件MD5
|
||||
string fileMD5 = MD5Generation(file);
|
||||
string fileName = Path.GetFileName(file);
|
||||
result[fileName] = MD5Generation(file);
|
||||
result[fileName] = fileMD5;
|
||||
|
||||
try
|
||||
{
|
||||
// 复制文件到fileFolder目录,以MD5值命名且不保留后缀
|
||||
string destPath = Path.Combine(fileFolder, fileMD5);
|
||||
File.Copy(file, destPath, overwrite: true);
|
||||
|
||||
Process_Box.Invoke((MethodInvoker)(() =>
|
||||
{
|
||||
Process_Box.AppendText($"已复制: {file} -> {destPath}{Environment.NewLine}");
|
||||
}));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Process_Box.Invoke((MethodInvoker)(() =>
|
||||
{
|
||||
Process_Box.AppendText($"复制文件失败 {file}: {ex.Message}{Environment.NewLine}");
|
||||
}));
|
||||
}
|
||||
|
||||
// 更新进度条
|
||||
processedFiles++;
|
||||
@ -124,8 +145,7 @@ namespace MD5Create
|
||||
continue;
|
||||
|
||||
string dirName = Path.GetFileName(dir);
|
||||
result[dirName] = GenerateDirectoryJson(dir, rootPath, totalFiles,
|
||||
ref processedFiles, excludeFolder);
|
||||
result[dirName] = GenerateDirectoryJson(dir, rootPath, totalFiles, ref processedFiles, excludeFolder, fileFolder);
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -156,10 +176,14 @@ namespace MD5Create
|
||||
{
|
||||
Process_Box.Clear();
|
||||
progressBar1.Value = 0;
|
||||
Application.DoEvents(); // 让UI立即更新
|
||||
Application.DoEvents();
|
||||
|
||||
// 存储MD5值
|
||||
string md5FolderPath = Path.Combine(rootDirectory, "MD5");
|
||||
Directory.CreateDirectory(md5FolderPath);
|
||||
// 存储MD5值对应的文件
|
||||
string fileFolderPath = Path.Combine(md5FolderPath, "File");
|
||||
Directory.CreateDirectory(fileFolderPath);
|
||||
|
||||
int totalFiles = GetTotalFileCount(rootDirectory, md5FolderPath);
|
||||
int processedFiles = 0;
|
||||
@ -172,7 +196,7 @@ namespace MD5Create
|
||||
return;
|
||||
}
|
||||
|
||||
var directoryStructure = GenerateDirectoryJson(rootDirectory, rootDirectory, totalFiles, ref processedFiles, md5FolderPath);
|
||||
var directoryStructure = GenerateDirectoryJson(rootDirectory, rootDirectory, totalFiles, ref processedFiles, md5FolderPath, fileFolderPath);
|
||||
|
||||
// 创建包含版本号的新结构
|
||||
var versionedStructure = new Dictionary<string, object>
|
||||
|
Loading…
x
Reference in New Issue
Block a user