在我寻找答案的过程中,我曾经看到过很多类似以下代码示例的内容:
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();
}
}
}
很遗憾,似乎Windows Server 2008没有SecurityCenter
或SecurityCenter2
命名空间,因此我尝试该方法时会发生Invalid namespace
异常。
有人知道如何确定Windows Server 2008是否正在运行防病毒软件吗?任何帮助将不胜感激!
return instances.Any();
而不是Count > 0
的习惯-如果instances
是一个非常长的列表,仅仅为了查看成员是否大于零而计算所有成员是一个不好的主意 :) - carlpettSecurityCenter
命名空间。 - wjhguitarman