我有一个 C# 控制台代码来启用和禁用 Windows Defender Real Time,如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
namespace enable_and_disable_real_time_protection
{
class Program
{
static void Main()
{
// تفعيل Real-time Protection
//SetRealTimeProtectionStatus(true);
// إلغاء تفعيل Real-time Protection
//SetRealTimeProtectionStatus(false);
// الحصول على حالة Real-time Protection
bool isRealTimeProtectionEnabled = GetRealTimeProtectionStatus();
Console.WriteLine("Real-time Protection Enabled: " + isRealTimeProtectionEnabled);
if (isRealTimeProtectionEnabled)
SetRealTimeProtectionStatus(false);
else
SetRealTimeProtectionStatus(true);
isRealTimeProtectionEnabled = GetRealTimeProtectionStatus();
Console.WriteLine("Real-time Protection Enabled: " + isRealTimeProtectionEnabled);
}
public static void SetRealTimeProtectionStatus(bool enable)
{
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.AddCommand("Set-MpPreference")
.AddParameter("DisableRealtimeMonitoring", !enable)
.Invoke();
}
}
public static bool GetRealTimeProtectionStatus()
{
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.AddCommand("Get-MpPreference")
.AddParameter("ErrorAction", "SilentlyContinue");
var psOutput = powerShell.Invoke();
foreach (var psObject in psOutput)
{
if (psObject.Properties["DisableRealtimeMonitoring"].Value != null)
{
return !(bool)psObject.Properties["DisableRealtimeMonitoring"].Value;
}
}
}
return false; // في حالة عدم القدرة على اكتشاف حالة Real-time Protection
}
}
}
该代码在 Windows 10 上完美运行,但在 Windows 11 上不起作用
当我在 Windows 11 中的 Windows Power Shell 上执行启用和禁用 Windows Defender Real Time 的命令时,它们对我不起作用。
我尝试以管理员身份运行该程序,但问题仍然仅存在于 Windows 11 中。我想在 Windows Server 上尝试该程序,看看是否存在相同的问题,但我无法使用。
我尝试了一切,但对我不起作用,有人可以帮助我吗