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