将移动临时文件修改为复制临时文件
This commit is contained in:
48
Form1.cs
48
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 尝试将文件从源位置复制到目标位置
|
||||
/// </summary>
|
||||
private async Task<bool> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user