有没有一种方法可以用C#检测计算机上是否安装了防病毒软件?我知道“安全中心”可以检测防病毒软件,但是如何在C#中检测呢?
根据微软的说法,Windows安全中心采用了两层检测状态的方法。一层是手动检测,另一层是通过Windows管理工具(WMI)自动检测。在手动检测模式下,Windows安全中心搜索由独立软件制造商提供给微软的注册表键和文件。这些注册表键和文件使Windows安全中心能够检测独立软件的状态。在WMI模式下,软件制造商确定其自己的产品状态,并通过WMI提供程序向Windows安全中心报告该状态。在这两种模式下,Windows安全中心都试图确定以下内容是否正确:
已经安装了防病毒程序。
防毒软件签名已更新。
对于防病毒程序,实时扫描或按需扫描是否已启用。
对于防火墙,Windows安全中心检测第三方防火墙是否已安装以及防火墙是否已打开。
因此,为了确定是否存在防病毒软件,可以使用WMI连接到root\SecurityCenter
命名空间(从Windows Vista开始必须使用root\SecurityCenter2
命名空间),然后查询AntiVirusProduct
WMI类。
请参考以下示例代码:
using System;
using System.Text;
using System.Management;
namespace ConsoleApplication1
{
class Program
{
public static bool AntivirusInstalled()
{
string wmipathstr = @"\\" + Environment.MachineName + @"\root\SecurityCenter";
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmipathstr, "SELECT * FROM AntivirusProduct");
ManagementObjectCollection instances = searcher.Get();
return instances.Count > 0;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return false;
}
public static void Main(string[] args)
{
bool returnCode = AntivirusInstalled();
Console.WriteLine("Antivirus Installed " + returnCode.ToString());
Console.WriteLine();
Console.Read();
}
}
}
\root\SecurityCenter2
命名空间。 - RRUZ使用记事本打开C:\Windows\System32\wbem\wscenter.mof
,可以帮助你查看存在哪些命名空间和类:
C#查询:
// SELECT * FROM AntiVirusProduct
// SELECT * FROM FirewallProduct
// SELECT * FROM AntiSpywareProduct
ManagementObjectSearcher wmiData = new ManagementObjectSearcher(@"root\SecurityCenter2", "SELECT * FROM AntiVirusProduct");
ManagementObjectCollection data = wmiData.Get();
foreach (ManagementObject virusChecker in data)
{
var virusCheckerName = virusChecker["displayName"];
}
wscenter.mof:
#pragma autorecover
#pragma classflags(64)
#pragma namespace("\\\\.\\root")
[NamespaceSecuritySDDL("O:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464G:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464D:(A;CI;0x1;;;BU)(A;CI;0x1;;;BA)(A;CI;0x1;;;NS)(A;CI;0x1;;;LS)(A;CI;0x1;;;AU)(A;CI;0x6001D;;;S-1-5-80-3232712927-1625117661-2590453128-1738570065-3637376297)")]
Instance of __namespace
{
Name = "SecurityCenter";
};
[NamespaceSecuritySDDL("O:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464G:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464D:(A;CI;0x1;;;BU)(A;CI;0x1;;;BA)(A;CI;0x1;;;NS)(A;CI;0x1;;;LS)(A;CI;0x1;;;AU)(A;CI;0x6001D;;;S-1-5-80-3232712927-1625117661-2590453128-1738570065-3637376297)")]
Instance of __namespace
{
Name = "SecurityCenter2";
};
#pragma namespace("\\\\.\\root\\SecurityCenter")
class AntiVirusProduct
{
[key,Not_Null] string instanceGuid;
[Not_Null] string displayName;
[Not_Null] boolean productUptoDate;
boolean onAccessScanningEnabled;
boolean productHasNotifiedUser;
boolean productWantsWscNotifications;
uint8 productState;
string companyName;
string versionNumber;
string pathToSignedProductExe;
};
class FirewallProduct
{
[key,Not_Null] string instanceGuid;
[Not_Null] string displayName;
boolean enabled;
boolean productHasNotifiedUser;
boolean productWantsWscNotifications;
uint8 productState;
string companyName;
string versionNumber;
string pathToSignedProductExe;
};
class AntiSpywareProduct
{
[key,Not_Null] string instanceGuid;
[Not_Null] string displayName;
[Not_Null] boolean productUptoDate;
boolean productEnabled;
boolean productHasNotifiedUser;
boolean productWantsWscNotifications;
uint8 productState;
string companyName;
string versionNumber;
string pathToSignedProductExe;
};
#pragma namespace("\\\\.\\root\\SecurityCenter2")
class AntiVirusProduct
{
[key,Not_Null] string instanceGuid;
[Not_Null] string displayName;
[Not_Null] string pathToSignedProductExe;
[Not_Null] string pathToSignedReportingExe;
[Not_Null] uint32 productState;
string timestamp;
};
class FirewallProduct
{
[key,Not_Null] string instanceGuid;
[Not_Null] string displayName;
[Not_Null] string pathToSignedProductExe;
[Not_Null] string pathToSignedReportingExe;
[Not_Null] uint32 productState;
string timestamp;
};
class AntiSpywareProduct
{
[key,Not_Null] string instanceGuid;
[Not_Null] string displayName;
[Not_Null] string pathToSignedProductExe;
[Not_Null] string pathToSignedReportingExe;
[Not_Null] uint32 productState;
string timestamp;
};
#pragma autorecover
在Vista SP2及更高版本中,WMI查询略有变化。
请尝试使用以下部分:\root\SecurityCenter2,而不是\root\SecurityCenter。
结果也略有不同。您仍然可以获取显示名称,但需要对ProductState字段进行一些位掩码操作,以确定onAccessScanner是否已启用/禁用以及最新信息的种类。