From 9084f35b8f5270d6453ca0642637c826facd3026 Mon Sep 17 00:00:00 2001
From: dong <1278815766@qq.com>
Date: Sat, 19 Jul 2025 18:24:19 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=A7=BB=E5=8A=A8=E9=97=AE?=
=?UTF-8?q?=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
CheckDownload.csproj | 6 +--
Form1.cs | 91 +++++++++++++++++++++++++++++++++++++++++---
2 files changed, 89 insertions(+), 8 deletions(-)
diff --git a/CheckDownload.csproj b/CheckDownload.csproj
index a32b34e..ab090a2 100644
--- a/CheckDownload.csproj
+++ b/CheckDownload.csproj
@@ -194,13 +194,13 @@
这台计算机上缺少此项目引用的 NuGet 程序包。使用"NuGet 程序包还原"可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。
-
-
+
+
+
-
\ No newline at end of file
diff --git a/Form1.cs b/Form1.cs
index a892c32..c9c1409 100644
--- a/Form1.cs
+++ b/Form1.cs
@@ -620,6 +620,7 @@ namespace CheckDownload
{
if (!File.Exists(tempFilePath))
{
+ //UpdateStatus($"[调试] 临时文件不存在: {fileName}");
return false;
}
@@ -641,6 +642,7 @@ namespace CheckDownload
{
var fileInfo = new FileInfo(tempFilePath);
Interlocked.Add(ref _totalDownloadedBytes, fileInfo.Length);
+ //UpdateStatus($"[调试] 临时文件验证通过,大小: {fileInfo.Length} bytes - {fileName}");
}
catch
{
@@ -650,10 +652,6 @@ namespace CheckDownload
}
else
{
- if (!string.IsNullOrWhiteSpace(fileName))
- {
- UpdateStatus($"临时文件不完整,重新下载: {fileName}");
- }
File.Delete(tempFilePath);
return false;
}
@@ -662,7 +660,7 @@ namespace CheckDownload
{
if (!string.IsNullOrWhiteSpace(fileName))
{
- UpdateStatus($"检查临时文件时出错,将重新下载: {fileName} - {ex.Message}");
+ UpdateStatus($"临时文件异常,删除文件: {fileName} - {ex.Message}");
}
try
{
@@ -917,6 +915,10 @@ namespace CheckDownload
UpdateStatus("正在校验文件...");
UpdateCount("");
UpdateSize("");
+
+ // 新增:在开始校验前创建临时文件备份
+ await CreateTempFileBackup();
+
var failedFiles = new ConcurrentBag();
var filesForScripting = new ConcurrentBag<(string original, string newFile)>();
@@ -933,9 +935,18 @@ namespace CheckDownload
string expectedMd5 = item.Value;
string tempFilePath = Path.Combine(_tempDirectory, relativePath);
+ // 添加调试信息:检查临时文件是否存在
+ if (!File.Exists(tempFilePath))
+ {
+ //UpdateStatus($"[调试] 临时文件在校验前消失: {relativePath}");
+ failedFiles.Add(relativePath);
+ return;
+ }
+
string actualMd5 = CalculateMD5FromFile(tempFilePath);
if (actualMd5 != expectedMd5.ToLower())
{
+ //UpdateStatus($"[调试] 文件MD5校验失败: {relativePath}");
failedFiles.Add(relativePath);
return;
}
@@ -947,12 +958,24 @@ namespace CheckDownload
Directory.CreateDirectory(localDir);
}
+ //UpdateStatus($"[调试] 开始复制文件: {relativePath}");
if (!await TryCopyFileAsync(tempFilePath, localPath)) // 改为使用复制方法
{
+ //UpdateStatus($"[调试] 直接复制失败,创建.new文件: {relativePath}");
string backupPath = localPath + ".new";
File.Copy(tempFilePath, backupPath, true); // 复制而非移动
filesForScripting.Add((localPath, backupPath));
}
+ else
+ {
+ //UpdateStatus($"[调试] 文件复制成功: {relativePath}");
+ }
+
+ // 复制后再次检查临时文件是否还存在
+ if (!File.Exists(tempFilePath))
+ {
+ //UpdateStatus($"[调试] 警告:文件复制后临时文件消失: {relativePath}");
+ }
}
finally
{
@@ -1874,5 +1897,63 @@ namespace CheckDownload
}
}
}
+
+ ///
+ /// 创建临时文件的备份,防止在程序退出后被删除
+ ///
+ private async Task CreateTempFileBackup()
+ {
+ try
+ {
+ string tempPath = Path.Combine(
+ Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
+ "Temp",
+ _appName
+ );
+
+ if (Directory.Exists(tempPath))
+ {
+ string backupDir = Path.Combine(_baseDirectory, "TempBackup");
+ if (!Directory.Exists(backupDir))
+ {
+ Directory.CreateDirectory(backupDir);
+ }
+
+ string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
+ string backupPath = Path.Combine(backupDir, $"Temp_{timestamp}");
+
+ // 简单复制整个临时目录
+ await Task.Run(() => CopyDirectory(tempPath, backupPath));
+
+ //UpdateStatus($"[调试] 临时文件已备份到: {backupPath}");
+ }
+ }
+ catch (Exception ex)
+ {
+ UpdateStatus($"创建临时文件备份失败: {ex.Message}");
+ }
+ }
+
+ ///
+ /// 递归复制目录
+ ///
+ private void CopyDirectory(string sourceDir, string targetDir)
+ {
+ Directory.CreateDirectory(targetDir);
+
+ foreach (var file in Directory.GetFiles(sourceDir))
+ {
+ string fileName = Path.GetFileName(file);
+ string targetFile = Path.Combine(targetDir, fileName);
+ File.Copy(file, targetFile, true);
+ }
+
+ foreach (var subDir in Directory.GetDirectories(sourceDir))
+ {
+ string dirName = Path.GetFileName(subDir);
+ string targetSubDir = Path.Combine(targetDir, dirName);
+ CopyDirectory(subDir, targetSubDir);
+ }
+ }
}
}
\ No newline at end of file