From a3708d496a9b989efd88b5be929cd7fe2063d11a Mon Sep 17 00:00:00 2001 From: dong <1278815766@qq.com> Date: Mon, 30 Jun 2025 21:20:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0OneDrive=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E5=AF=86=E9=92=A5=EF=BC=8C=E4=BC=98=E5=8C=96.exe=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E6=8E=92=E9=99=A4=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=89=B9=E5=A4=84=E7=90=86?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E4=BB=A5=E8=AF=B7=E6=B1=82=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=91=98=E6=9D=83=E9=99=90=E6=B7=BB=E5=8A=A0=E6=8E=92=E9=99=A4?= =?UTF-8?q?=E9=A1=B9=EF=BC=8C=E6=94=B9=E8=BF=9B=E7=8A=B6=E6=80=81=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Form1.cs | 131 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 81 insertions(+), 50 deletions(-) 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