修改了结束exe程序的位置

This commit is contained in:
2025-07-18 17:22:23 +08:00
parent 9b790fe9e0
commit 848de6f3fa

114
Form1.cs
View File

@@ -42,11 +42,11 @@ namespace CheckDownload
// 阿里云OSS访问密钥Secret // 阿里云OSS访问密钥Secret
private const string OssAccessKeySecret = "7ClQns3wz6psmIp9T2OfuEn3tpzrCK"; private const string OssAccessKeySecret = "7ClQns3wz6psmIp9T2OfuEn3tpzrCK";
// 123盘鉴权密钥 // 123盘鉴权密钥
private const string OneDriveAuthKey = "6SwdpWdSJuJRSh"; private const string OneDriveAuthKey = "ZhwG3LxOtGJwM3ym";
// 123盘UID // 123盘UID
private const string OneDriveUid = "1826795402"; private const string OneDriveUid = "1850250683";
// 123盘路径不包含域名- 修改此处即可同时生效于主备域名 // 123盘路径不包含域名- 修改此处即可同时生效于主备域名
private const string OneDrivePath = "/1826795402/KeyAuth"; private const string OneDrivePath = "/1850250683/SuWin";
// 123盘主域名 // 123盘主域名
private const string OneDriveMainDomain = "vip.123pan.cn"; private const string OneDriveMainDomain = "vip.123pan.cn";
// 123盘备用域名 // 123盘备用域名
@@ -520,12 +520,6 @@ namespace CheckDownload
/// <returns>下载成功返回true否则返回false</returns> /// <returns>下载成功返回true否则返回false</returns>
private async Task<bool> AttemptDownloadAsync(string filePath, string expectedMd5) private async Task<bool> 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 tempFilePath = Path.Combine(_tempDirectory, filePath);
string fileName = Path.GetFileName(filePath); string fileName = Path.GetFileName(filePath);
string truncatedFileName = TruncateString(fileName, 10); string truncatedFileName = TruncateString(fileName, 10);
@@ -929,11 +923,11 @@ namespace CheckDownload
Directory.CreateDirectory(localDir); Directory.CreateDirectory(localDir);
} }
if (!await TryCopyFileAsync(tempFilePath, localPath)) // 改为使用复制方法 if (!await TryCopyFileAsync(tempFilePath, localPath)) // 改为使用复制方法
{ {
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));
} }
} }
finally finally
@@ -1623,12 +1617,22 @@ namespace CheckDownload
return ext == ".db" || ext == ".db3"; return ext == ".db" || ext == ".db3";
} }
private void KillProcessIfRunning(string exeRelativePath) private void KillProcessIfRunning(string exePath)
{ {
try try
{ {
string exeName = Path.GetFileNameWithoutExtension(exeRelativePath); string exeName = Path.GetFileNameWithoutExtension(exePath);
string targetExeDir = Path.GetDirectoryName(Path.Combine(_baseDirectory, exeRelativePath)); 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)) foreach (var proc in Process.GetProcessesByName(exeName))
{ {
@@ -1720,41 +1724,47 @@ namespace CheckDownload
string errorMessage = innermostException?.Message ?? ex?.Message ?? "发生未知错误。"; string errorMessage = innermostException?.Message ?? ex?.Message ?? "发生未知错误。";
MessageBox.Show(errorMessage, "更新错误", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(errorMessage, "更新错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
/// <summary> /// <summary>
/// 尝试将文件从源位置复制到目标位置 /// 尝试将文件从源位置复制到目标位置
/// </summary> /// </summary>
private async Task<bool> TryCopyFileAsync(string sourcePath, string targetPath) private async Task<bool> TryCopyFileAsync(string sourcePath, string targetPath)
{ {
try try
{ {
// 确保目标目录存在 // 如果目标文件是.exe文件先检查并kill掉可能正在运行的进程
string targetDir = Path.GetDirectoryName(targetPath); if (Path.GetExtension(targetPath).Equals(".exe", StringComparison.OrdinalIgnoreCase))
if (!Directory.Exists(targetDir)) {
{ KillProcessIfRunning(targetPath);
Directory.CreateDirectory(targetDir); }
}
// 确保目标目录存在
// 执行复制操作 string targetDir = Path.GetDirectoryName(targetPath);
File.Copy(sourcePath, targetPath, true); if (!Directory.Exists(targetDir))
return true; {
} Directory.CreateDirectory(targetDir);
catch (Exception ex) }
{
// 文件可能被占用,稍后重试 // 执行复制操作
try File.Copy(sourcePath, targetPath, true);
{ return true;
await Task.Delay(1000); }
File.Copy(sourcePath, targetPath, true); catch (Exception ex)
return true; {
} // 文件可能被占用,稍后重试
catch try
{ {
UpdateStatus($"文件复制失败: {Path.GetFileName(targetPath)}"); await Task.Delay(1000);
return false; File.Copy(sourcePath, targetPath, true);
} return true;
} }
catch
{
UpdateStatus($"文件复制失败: {Path.GetFileName(targetPath)}");
return false;
}
}
} }
} }
} }