From 0b8d48e714d3a4e88125e3e424614a931d18eb5a Mon Sep 17 00:00:00 2001 From: dong <1278815766@qq.com> Date: Mon, 30 Jun 2025 01:10:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B0=86.exe=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=B7=AF=E5=BE=84=E6=B7=BB=E5=8A=A0=E5=88=B0Windows?= =?UTF-8?q?=20Defender=E6=8E=92=E9=99=A4=E9=A1=B9=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E5=90=8E=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Form1.cs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/Form1.cs b/Form1.cs index 6d333db..37f32eb 100644 --- a/Form1.cs +++ b/Form1.cs @@ -912,7 +912,14 @@ namespace CheckDownload File.Move(tempFilePath, backupPath); filesForScripting.Add((localPath, backupPath)); } - + else + { + // 文件移动成功,如果是exe,则添加到信任区 + if (localPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) + { + AddToWindowsDefenderExclusion(localPath); + } + } } finally { @@ -1631,6 +1638,44 @@ 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到临时目录。 /// @@ -1716,4 +1761,5 @@ namespace CheckDownload catch { } } } +} } \ No newline at end of file