文件移动问题
This commit is contained in:
@@ -194,13 +194,13 @@
|
|||||||
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用"NuGet 程序包还原"可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
|
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用"NuGet 程序包还原"可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<Error Condition="!Exists('packages\Fody.6.9.2\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.6.9.2\build\Fody.targets'))" />
|
<Error Condition="!Exists('packages\Fody.6.9.2\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.6.9.2\build\Fody.targets'))" />
|
||||||
<!-- <Error Condition="!Exists('packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets'))" /> -->
|
|
||||||
<!-- <Error Condition="!Exists('packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets'))" /> -->
|
|
||||||
<Error Condition="!Exists('packages\Costura.Fody.6.0.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.6.0.0\build\Costura.Fody.props'))" />
|
<Error Condition="!Exists('packages\Costura.Fody.6.0.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.6.0.0\build\Costura.Fody.props'))" />
|
||||||
<Error Condition="!Exists('packages\Costura.Fody.6.0.0\build\Costura.Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.6.0.0\build\Costura.Fody.targets'))" />
|
<Error Condition="!Exists('packages\Costura.Fody.6.0.0\build\Costura.Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.6.0.0\build\Costura.Fody.targets'))" />
|
||||||
|
<!-- <Error Condition="!Exists('packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets'))" /> -->
|
||||||
|
<!-- <Error Condition="!Exists('packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets'))" /> -->
|
||||||
</Target>
|
</Target>
|
||||||
<Import Project="packages\Fody.6.9.2\build\Fody.targets" Condition="Exists('packages\Fody.6.9.2\build\Fody.targets')" />
|
<Import Project="packages\Fody.6.9.2\build\Fody.targets" Condition="Exists('packages\Fody.6.9.2\build\Fody.targets')" />
|
||||||
|
<Import Project="packages\Costura.Fody.6.0.0\build\Costura.Fody.targets" Condition="Exists('packages\Costura.Fody.6.0.0\build\Costura.Fody.targets')" />
|
||||||
<!-- <Import Project="packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets" Condition="Exists('packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets')" /> -->
|
<!-- <Import Project="packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets" Condition="Exists('packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets')" /> -->
|
||||||
<!-- <Import Project="packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets" Condition="Exists('packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets')" /> -->
|
<!-- <Import Project="packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets" Condition="Exists('packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets')" /> -->
|
||||||
<Import Project="packages\Costura.Fody.6.0.0\build\Costura.Fody.targets" Condition="Exists('packages\Costura.Fody.6.0.0\build\Costura.Fody.targets')" />
|
|
||||||
</Project>
|
</Project>
|
91
Form1.cs
91
Form1.cs
@@ -620,6 +620,7 @@ namespace CheckDownload
|
|||||||
{
|
{
|
||||||
if (!File.Exists(tempFilePath))
|
if (!File.Exists(tempFilePath))
|
||||||
{
|
{
|
||||||
|
//UpdateStatus($"[调试] 临时文件不存在: {fileName}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -641,6 +642,7 @@ namespace CheckDownload
|
|||||||
{
|
{
|
||||||
var fileInfo = new FileInfo(tempFilePath);
|
var fileInfo = new FileInfo(tempFilePath);
|
||||||
Interlocked.Add(ref _totalDownloadedBytes, fileInfo.Length);
|
Interlocked.Add(ref _totalDownloadedBytes, fileInfo.Length);
|
||||||
|
//UpdateStatus($"[调试] 临时文件验证通过,大小: {fileInfo.Length} bytes - {fileName}");
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
@@ -650,10 +652,6 @@ namespace CheckDownload
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(fileName))
|
|
||||||
{
|
|
||||||
UpdateStatus($"临时文件不完整,重新下载: {fileName}");
|
|
||||||
}
|
|
||||||
File.Delete(tempFilePath);
|
File.Delete(tempFilePath);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -662,7 +660,7 @@ namespace CheckDownload
|
|||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(fileName))
|
if (!string.IsNullOrWhiteSpace(fileName))
|
||||||
{
|
{
|
||||||
UpdateStatus($"检查临时文件时出错,将重新下载: {fileName} - {ex.Message}");
|
UpdateStatus($"临时文件异常,删除文件: {fileName} - {ex.Message}");
|
||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -917,6 +915,10 @@ namespace CheckDownload
|
|||||||
UpdateStatus("正在校验文件...");
|
UpdateStatus("正在校验文件...");
|
||||||
UpdateCount("");
|
UpdateCount("");
|
||||||
UpdateSize("");
|
UpdateSize("");
|
||||||
|
|
||||||
|
// 新增:在开始校验前创建临时文件备份
|
||||||
|
await CreateTempFileBackup();
|
||||||
|
|
||||||
var failedFiles = new ConcurrentBag<string>();
|
var failedFiles = new ConcurrentBag<string>();
|
||||||
var filesForScripting = new ConcurrentBag<(string original, string newFile)>();
|
var filesForScripting = new ConcurrentBag<(string original, string newFile)>();
|
||||||
|
|
||||||
@@ -933,9 +935,18 @@ namespace CheckDownload
|
|||||||
string expectedMd5 = item.Value;
|
string expectedMd5 = item.Value;
|
||||||
string tempFilePath = Path.Combine(_tempDirectory, relativePath);
|
string tempFilePath = Path.Combine(_tempDirectory, relativePath);
|
||||||
|
|
||||||
|
// 添加调试信息:检查临时文件是否存在
|
||||||
|
if (!File.Exists(tempFilePath))
|
||||||
|
{
|
||||||
|
//UpdateStatus($"[调试] 临时文件在校验前消失: {relativePath}");
|
||||||
|
failedFiles.Add(relativePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
string actualMd5 = CalculateMD5FromFile(tempFilePath);
|
string actualMd5 = CalculateMD5FromFile(tempFilePath);
|
||||||
if (actualMd5 != expectedMd5.ToLower())
|
if (actualMd5 != expectedMd5.ToLower())
|
||||||
{
|
{
|
||||||
|
//UpdateStatus($"[调试] 文件MD5校验失败: {relativePath}");
|
||||||
failedFiles.Add(relativePath);
|
failedFiles.Add(relativePath);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -947,12 +958,24 @@ namespace CheckDownload
|
|||||||
Directory.CreateDirectory(localDir);
|
Directory.CreateDirectory(localDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//UpdateStatus($"[调试] 开始复制文件: {relativePath}");
|
||||||
if (!await TryCopyFileAsync(tempFilePath, localPath)) // 改为使用复制方法
|
if (!await TryCopyFileAsync(tempFilePath, localPath)) // 改为使用复制方法
|
||||||
{
|
{
|
||||||
|
//UpdateStatus($"[调试] 直接复制失败,创建.new文件: {relativePath}");
|
||||||
string backupPath = localPath + ".new";
|
string backupPath = localPath + ".new";
|
||||||
File.Copy(tempFilePath, backupPath, true); // 复制而非移动
|
File.Copy(tempFilePath, backupPath, true); // 复制而非移动
|
||||||
filesForScripting.Add((localPath, backupPath));
|
filesForScripting.Add((localPath, backupPath));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//UpdateStatus($"[调试] 文件复制成功: {relativePath}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 复制后再次检查临时文件是否还存在
|
||||||
|
if (!File.Exists(tempFilePath))
|
||||||
|
{
|
||||||
|
//UpdateStatus($"[调试] 警告:文件复制后临时文件消失: {relativePath}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -1874,5 +1897,63 @@ namespace CheckDownload
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建临时文件的备份,防止在程序退出后被删除
|
||||||
|
/// </summary>
|
||||||
|
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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 递归复制目录
|
||||||
|
/// </summary>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user