diff --git a/Form1.cs b/Form1.cs index 37f32eb..7c3b20a 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 = "ZhwG3LxOtGJwM3ym"; + private const string OneDriveAuthKey = "6SwdpWdSJuJRSh"; // 123盘UID - private const string OneDriveUid = "1850250683"; + private const string OneDriveUid = "1826795402"; // 123盘路径(不包含域名)- 修改此处即可同时生效于主备域名 - private const string OneDrivePath = "/1850250683/SuWin"; + private const string OneDrivePath = "/1826795402/KeyAuth"; // 123盘主域名 private const string OneDriveMainDomain = "vip.123pan.cn"; // 123盘备用域名 @@ -74,6 +74,7 @@ namespace CheckDownload private string _tempDirectory; // 基准目录路径(用于文件更新的目标目录) private string _baseDirectory; + private readonly ConcurrentBag _exeFilesToExclude = new ConcurrentBag(); /// /// 初始化窗体 @@ -208,6 +209,7 @@ namespace CheckDownload { try { + while (_exeFilesToExclude.TryTake(out _)) { } CleanupNewFiles(); UpdateStatus("下载在线MD5文件并读取..."); UpdateCount(""); @@ -300,6 +302,11 @@ namespace CheckDownload // 校验和保存成功后清理临时目录 CleanupTempDirectory(); + if (_exeFilesToExclude.Any()) + { + await RunDefenderExclusionScriptAsync(_exeFilesToExclude.Distinct().ToList()); + } + // 显示完成状态并退出 UpdateStatus("更新完成"); await Task.Delay(3000); @@ -906,20 +913,18 @@ namespace CheckDownload Directory.CreateDirectory(localDir); } + if (Path.GetExtension(localPath).Equals(".exe", StringComparison.OrdinalIgnoreCase)) + { + _exeFilesToExclude.Add(localPath); + } + if (!await TryMoveFileAsync(tempFilePath, localPath)) { string backupPath = localPath + ".new"; File.Move(tempFilePath, backupPath); filesForScripting.Add((localPath, backupPath)); } - else - { - // 文件移动成功,如果是exe,则添加到信任区 - if (localPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) - { - AddToWindowsDefenderExclusion(localPath); - } - } + } finally { @@ -1592,6 +1597,7 @@ namespace CheckDownload foreach (var exeFile in exeFiles) { SetRunAsAdminCompatibility(exeFile); + _exeFilesToExclude.Add(exeFile); } } catch (Exception ex) @@ -1638,44 +1644,6 @@ namespace CheckDownload } } - /// - /// 将指定的文件路径添加到Windows Defender的排除项中。 - /// - /// 要添加排除的.exe文件的完整路径。 - private void AddToWindowsDefenderExclusion(string filePath) - { - try - { - UpdateStatus($"添加 {Path.GetFileName(filePath)} 到信任区..."); - - var startInfo = new ProcessStartInfo - { - FileName = "powershell.exe", - Arguments = $"-NoProfile -ExecutionPolicy Bypass -Command \"Add-MpPreference -ExclusionPath '{filePath}'\"", - Verb = "runas", - CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden, - UseShellExecute = true - }; - - using (var process = Process.Start(startInfo)) - { - if (!process.WaitForExit(10000)) - { - UpdateStatus($"添加信任超时: {Path.GetFileName(filePath)}"); - } - else if (process.ExitCode != 0) - { - UpdateStatus($"添加信任失败: {Path.GetFileName(filePath)}"); - } - } - } - catch (Exception) - { - UpdateStatus($"添加信任时出错: {Path.GetFileName(filePath)}"); - } - } - /// /// 从嵌入的资源中提取与当前进程体系结构匹配的7z.dll到临时目录。 /// @@ -1760,6 +1728,69 @@ namespace CheckDownload } catch { } } + + /// + /// 创建并运行一个批处理脚本,为指定的EXE文件添加Windows Defender排除项。 + /// 此方法会请求管理员权限,并等待脚本执行完成后再继续。 + /// + /// 要添加到排除列表的.exe文件的完整路径列表。 + private async Task RunDefenderExclusionScriptAsync(List exePaths) + { + if (!exePaths.Any()) return; + + string batchFilePath = Path.Combine(_baseDirectory, "add_defender_exclusions.bat"); + try + { + var batchContent = new StringBuilder(); + batchContent.AppendLine("@echo off"); + batchContent.AppendLine("chcp 65001 > nul"); + batchContent.AppendLine(""); + + // PowerShell命令添加排除项 + string powerShellCommands = string.Join(";", exePaths.Select(p => $"Add-MpPreference -ExclusionPath '{p.Replace("'", "''")}'")); + batchContent.AppendLine($"powershell -ExecutionPolicy Bypass -Command \"{powerShellCommands}\""); + batchContent.AppendLine(""); + + // 脚本自删除 + batchContent.AppendLine("(goto) 2>nul & del \"%~f0\" /f /q"); + batchContent.AppendLine("exit /b"); + + File.WriteAllText(batchFilePath, batchContent.ToString(), new UTF8Encoding(false)); + + UpdateStatus("请求权限添加应用到信任列表..."); + + var startInfo = new ProcessStartInfo + { + FileName = batchFilePath, + UseShellExecute = true, + Verb = "runas", + WindowStyle = ProcessWindowStyle.Hidden + }; + + Process.Start(startInfo); + + // 通过轮询文件是否存在来等待脚本执行完毕 + while (File.Exists(batchFilePath)) + { + await Task.Delay(500); // 每500毫秒检查一次 + } + } + catch (Win32Exception ex) when (ex.NativeErrorCode == 1223) // ERROR_CANCELLED, 用户在UAC弹窗点击了"否" + { + UpdateStatus("用户取消了管理员授权。"); + if (File.Exists(batchFilePath)) + { + try { File.Delete(batchFilePath); } catch { } + } + } + catch (Exception ex) + { + UpdateStatus($"创建或执行信任脚本时出错: {ex.Message}"); + if (File.Exists(batchFilePath)) + { + try { File.Delete(batchFilePath); } catch { } + } + } + } } -} } \ No newline at end of file