This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss

在C#中使用WMI Query Language查询信息

1080 views 0 comments last modified about 6 years ago Raymond

WMI .NET API

在.NET中我们使用标准的WMI查询语句查询电脑上的相关信息。WQL是SQL的一个子集。在.NET中所有相关的类(如ManagementObjectSearcher)封装在System.Management命名空间中。具体参考MSDN相关帮助文档http://msdn.microsoft.com/en-us/library/ms186146.aspx

本文将采用C#检测电脑上安装的杀毒软件信息。

WMI Administrative Tools

为了保证查询到相关的信息,请先安装WML管理工具WMI Administrative Tools。安装后使用WMI Object Browser来检测你需要查找的实例是否存在或者路径是否正确。

如下图所示,在我的PC上我的杀毒软件信息存在路径是root\SecurityCenter2 而非 root\SecurityCenter

image

C#代码查询

使用以下代码即可查询出相关的信息:

   1:  private static void UsingWMI()
   2:          {
   3:              WqlObjectQuery wqlQuery = new WqlObjectQuery("SELECT * FROM AntiVirusProduct");
   4:   
   5:              string computer = Environment.MachineName;   
   6:              string path = @"\\" + computer + @"\root\SecurityCenter2";   
   7:   
   8:              ManagementScope scope = new ManagementScope
   9:              {
  10:                  Path = new ManagementPath(path)
  11:              };
  12:              ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, wqlQuery);
  13:   
  14:              ManagementObjectCollection instances = searcher.Get();
  15:              foreach (ManagementObject obj in instances)
  16:              {
  17:                  foreach (var property in obj.Properties)
  18:                  {
  19:                      Console.WriteLine("{0}:{1}", property.Name, property.Value);
  20:                  }
  21:              }
  22:          }

在我的PC上可以查询出我安装了Avast以及Windows Defender.

image

Related pages

Set AttachDbFilename as Relative Path in .NET Core

317 views   0 comments last modified about 5 months ago

.NET Framework, you can use |DataDirectory| to configure connection string when connecting to SQL Server database file via attach mode: AttachDbFilename=|DataDirectory|\dbname.mdf In .NET Core, you cannot directly set SQL Server Express connec...

View detail

Instantiate a Service in ConfigureServices Method in .NET Core

45 views   0 comments last modified about 5 months ago

.NET Core is built in with dependency injection. Usually method ConfigureServices in Startup class is used to register services in the container. The signature of the method looks like the following: public void ConfigureServices(IServiceC...

View detail

ASP.NET Core 2.1 Error - 'Cyrillic' is not a supported encoding name

279 views   0 comments last modified about 5 months ago

After upgrading to ASP.NET Core 2.1 (.NET Core SDK 2.1.301), you may encounter the following error about encoding: System.ArgumentException    HResult=0x80070057    Message='Cyrillic' is not a supported encoding name. For information on defining a custo...

View detail

SQLite in .NET Core with Entity Framework Core

173 views   0 comments last modified about 5 months ago

SQLite is a self-contained and embedded SQL database engine. In .NET Core, Entity Framework Core provides APIs to work with SQLite. This page provides sample code to create a SQLite database using package Microsoft.EntityFrameworkCore.Sqlite . Create sample project ...

View detail

Tuples in C# (4.x, 7.0, 7.1)

195 views   0 comments last modified about 9 months ago

What is a tuple? Tuple is an ordered sequence, fixed-size, immutable and of heterogeneous objects. Tuple has been existing in other languages such as F#, Python, Perl and etc. for a long time. It was first introduced into C# from C# 4.0 and has been evolving over time. Since C# 7.1, tuple...

View detail

C# version history and core features

149 views   0 comments last modified about 9 months ago

C# is my favourite programming language and has been evolving overtime. I have been programming with many other languages but C# is always my favourite because it is simple and easy to use. Visual Studio is the most powerful tool I’ve ever used. To me, programming efficiency and peformance are th...

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.