我需要更好的方式来查询已安装的防病毒软件。在c#中这样的事情也许?
问题描述:
是否有C#等同于此?我尝试过使用WMI,只需获取“Windows Defender”,而不管已安装的WMI兼容AntiVirus。我需要更好的方式来查询已安装的防病毒软件。在c#中这样的事情也许?
我只是想在文本框中显示这些结果。
WMIC /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct Get displayName /Format:List
当我使用上面的代码时,我得到了我的防病毒软件的实际名称。
答
您可以添加对System.Management
的引用。然后使用ManagementObjectSearcher
可以运行WMI查询。
要查找已安装的防病毒软件,您应该搜索SecurityCenter2
。例如:
var path = string.Format(@"\\{0}\root\SecurityCenter2", Environment.MachineName);
var searcher = new ManagementObjectSearcher(path, "SELECT * FROM AntivirusProduct");
var instances = searcher.Get().Cast<ManagementObject>()
.Select(x => (string)x.GetPropertyValue("displayName"))
.ToList();
注1:对于Windows XP,在SecurityCenter
搜索。
注2:你也可以阅读AntiVirusProduct
的其他属性:
-
displayName
:string
-
instanceGuid
:string
-
pathToSignedProductExe
:string
-
pathToSignedReportingExe
:string
-
productState
:UInt32
。 (有关如何解析状态信息,看看this post。) -
timestamp
:string
我怎么能分析出结果的文本框?我在WMIC结果中也遇到了这个问题,并且不记得我是如何修复它的。lol –
在'TextBox'中显示一个'List'不是一个好主意,最好是将它显示在ListBox中,例如通过设置' listBox1.DataSource = instances;'。无论如何,如果你想在'TextBox'中显示,你可以使用'textBox1.Text = string.Join(Environment.NewLine,instances);'。 –
omg我很尴尬,你是完全正确的。我用每个语句来查询整个结果并发布到一个列表框中... –