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