修改了结束exe程序的位置
This commit is contained in:
114
Form1.cs
114
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
|
||||
/// <returns>下载成功返回true,否则返回false</returns>
|
||||
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 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);
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 尝试将文件从源位置复制到目标位置
|
||||
/// </summary>
|
||||
private async Task<bool> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user