添加将.exe文件路径添加到Windows Defender排除项的功能,优化文件移动后的处理逻辑
This commit is contained in:
48
Form1.cs
48
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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将指定的文件路径添加到Windows Defender的排除项中。
|
||||
/// </summary>
|
||||
/// <param name="filePath">要添加排除的.exe文件的完整路径。</param>
|
||||
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)}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从嵌入的资源中提取与当前进程体系结构匹配的7z.dll到临时目录。
|
||||
/// </summary>
|
||||
@@ -1717,3 +1762,4 @@ namespace CheckDownload
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user