From 9b790fe9e03b10dc30d412fd4118605ccda4f4dd Mon Sep 17 00:00:00 2001 From: dong <1278815766@qq.com> Date: Fri, 18 Jul 2025 17:10:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E7=A7=BB=E5=8A=A8=E4=B8=B4=E6=97=B6?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=BF=AE=E6=94=B9=E4=B8=BA=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Form1.cs | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/Form1.cs b/Form1.cs index 313b19e..beb74a6 100644 --- a/Form1.cs +++ b/Form1.cs @@ -929,13 +929,12 @@ namespace CheckDownload Directory.CreateDirectory(localDir); } - if (!await TryMoveFileAsync(tempFilePath, localPath)) - { - string backupPath = localPath + ".new"; - File.Move(tempFilePath, backupPath); - filesForScripting.Add((localPath, backupPath)); + if (!await TryCopyFileAsync(tempFilePath, localPath)) // 改为使用复制方法 + { + string backupPath = localPath + ".new"; + File.Copy(tempFilePath, backupPath, true); // 复制而非移动 + filesForScripting.Add((localPath, backupPath)); } - } finally { @@ -1038,7 +1037,7 @@ namespace CheckDownload foreach (var file in files) { batchContent.AppendLine($"del \"{file.original}\" /f /q"); - batchContent.AppendLine($"move \"{file.newFile}\" \"{file.original}\""); + batchContent.AppendLine($"copy /Y \"{file.newFile}\" \"{file.original}\""); } batchContent.AppendLine("del \"%~f0\" /f /q"); @@ -1721,6 +1720,41 @@ namespace CheckDownload string errorMessage = innermostException?.Message ?? ex?.Message ?? "发生未知错误。"; MessageBox.Show(errorMessage, "更新错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + /// + /// 尝试将文件从源位置复制到目标位置 + /// + private async Task TryCopyFileAsync(string sourcePath, string targetPath) + { + try + { + // 确保目标目录存在 + string targetDir = Path.GetDirectoryName(targetPath); + if (!Directory.Exists(targetDir)) + { + Directory.CreateDirectory(targetDir); + } + + // 执行复制操作 + File.Copy(sourcePath, targetPath, true); + return true; + } + catch (Exception ex) + { + // 文件可能被占用,稍后重试 + try + { + await Task.Delay(1000); + File.Copy(sourcePath, targetPath, true); + return true; + } + catch + { + UpdateStatus($"文件复制失败: {Path.GetFileName(targetPath)}"); + return false; + } + } } } } \ No newline at end of file