From 848de6f3fa17f354bc376c11217e9d26a3c0139c Mon Sep 17 00:00:00 2001
From: dong <1278815766@qq.com>
Date: Fri, 18 Jul 2025 17:22:23 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E7=BB=93=E6=9D=9Fex?=
=?UTF-8?q?e=E7=A8=8B=E5=BA=8F=E7=9A=84=E4=BD=8D=E7=BD=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Form1.cs | 114 ++++++++++++++++++++++++++++++-------------------------
1 file changed, 62 insertions(+), 52 deletions(-)
diff --git a/Form1.cs b/Form1.cs
index beb74a6..d135e4b 100644
--- a/Form1.cs
+++ b/Form1.cs
@@ -42,11 +42,11 @@ namespace CheckDownload
// 阿里云OSS访问密钥Secret
private const string OssAccessKeySecret = "7ClQns3wz6psmIp9T2OfuEn3tpzrCK";
// 123盘鉴权密钥
- private const string OneDriveAuthKey = "6SwdpWdSJuJRSh";
+ private const string OneDriveAuthKey = "ZhwG3LxOtGJwM3ym";
// 123盘UID
- private const string OneDriveUid = "1826795402";
+ private const string OneDriveUid = "1850250683";
// 123盘路径(不包含域名)- 修改此处即可同时生效于主备域名
- private const string OneDrivePath = "/1826795402/KeyAuth";
+ private const string OneDrivePath = "/1850250683/SuWin";
// 123盘主域名
private const string OneDriveMainDomain = "vip.123pan.cn";
// 123盘备用域名
@@ -520,12 +520,6 @@ namespace CheckDownload
/// 下载成功返回true,否则返回false
private async Task AttemptDownloadAsync(string filePath, string expectedMd5)
{
- // kill exe if running
- if (Path.GetExtension(filePath).Equals(".exe", StringComparison.OrdinalIgnoreCase))
- {
- KillProcessIfRunning(filePath);
- }
-
string tempFilePath = Path.Combine(_tempDirectory, filePath);
string fileName = Path.GetFileName(filePath);
string truncatedFileName = TruncateString(fileName, 10);
@@ -929,11 +923,11 @@ namespace CheckDownload
Directory.CreateDirectory(localDir);
}
- if (!await TryCopyFileAsync(tempFilePath, localPath)) // 改为使用复制方法
- {
- string backupPath = localPath + ".new";
- File.Copy(tempFilePath, backupPath, true); // 复制而非移动
- filesForScripting.Add((localPath, backupPath));
+ if (!await TryCopyFileAsync(tempFilePath, localPath)) // 改为使用复制方法
+ {
+ string backupPath = localPath + ".new";
+ File.Copy(tempFilePath, backupPath, true); // 复制而非移动
+ filesForScripting.Add((localPath, backupPath));
}
}
finally
@@ -1623,12 +1617,22 @@ namespace CheckDownload
return ext == ".db" || ext == ".db3";
}
- private void KillProcessIfRunning(string exeRelativePath)
+ private void KillProcessIfRunning(string exePath)
{
try
{
- string exeName = Path.GetFileNameWithoutExtension(exeRelativePath);
- string targetExeDir = Path.GetDirectoryName(Path.Combine(_baseDirectory, exeRelativePath));
+ string exeName = Path.GetFileNameWithoutExtension(exePath);
+ string targetExeDir;
+
+ // 判断是否为完整路径
+ if (Path.IsPathRooted(exePath))
+ {
+ targetExeDir = Path.GetDirectoryName(exePath);
+ }
+ else
+ {
+ targetExeDir = Path.GetDirectoryName(Path.Combine(_baseDirectory, exePath));
+ }
foreach (var proc in Process.GetProcessesByName(exeName))
{
@@ -1720,41 +1724,47 @@ 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;
- }
- }
+ }
+
+ ///
+ /// 尝试将文件从源位置复制到目标位置
+ ///
+ private async Task TryCopyFileAsync(string sourcePath, string targetPath)
+ {
+ try
+ {
+ // 如果目标文件是.exe文件,先检查并kill掉可能正在运行的进程
+ if (Path.GetExtension(targetPath).Equals(".exe", StringComparison.OrdinalIgnoreCase))
+ {
+ KillProcessIfRunning(targetPath);
+ }
+
+ // 确保目标目录存在
+ 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