From d7fd84190629ebeda8c15bab350a9b7be4d3aa20 Mon Sep 17 00:00:00 2001 From: Dong <1278815766@qq.com> Date: Wed, 7 May 2025 14:32:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=A8=E7=94=9F=E6=88=90=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=BB=93=E6=9E=84=E6=97=B6=E6=B7=BB=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=8D=E5=88=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在生成目录结构的 json 表示时,新增了将文件复制到指定目录并以 MD5 值命名的功能。同时,创建了用于存储 MD5 值对应文件的目录,并在 UI 中显示复制过程的状态。 --- Form1.cs | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/Form1.cs b/Form1.cs index 357ed88..0262a5a 100644 --- a/Form1.cs +++ b/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 GenerateDirectoryJson(string directoryPath, string rootPath, - int totalFiles, ref int processedFiles, string excludeFolder) + private Dictionary GenerateDirectoryJson(string directoryPath, string rootPath, int totalFiles, ref int processedFiles, string excludeFolder, string fileFolder) { var result = new Dictionary(); // 处理当前目录文件 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