WMI Queries

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

You can use standard WMI queries in .NET Framework applications. WMI queries use WMI Query Language (WQL), a subset of SQL. The query string is stored in a .NET Framework query class such as the SelectQuery class, the WqlEventQuery class, or the RelatedObjectQuery class, and invoked by the ManagementObjectSearcher or ManagementEventWatcher class constructors.

For more information, see "Querying with WQL" in the Windows Management Instrumentation documentation in the MSDN Library at http://msdn.microsoft.com/library.

Queries limit the amount of data being returned by including:

  • The SELECT clause to specify only the properties for which you want data returned.

  • The WHERE clause to specify which instances you want returned.

As an example, the following query selects all the drives on a computer (drive C, D, and so on) that have less than 2 MB of free space:

SELECT * FROM Win32_LogicalDisk WHERE FreeSpace < 2000000

Types of Query Classes

WMI in .NET Framework has a number of query classes in the Framework schema. Some of these perform certain types of WQL queries, such as WQL SELECT queries or WQL ASSOCIATORS OF queries.

The following table shows the query classes you can use to build queries of the WMI data.

.NET Framework class Description

EventQuery

A WMI event query. Objects of this class or its derivatives are used in the constructor of the ManagementEventWatcher class to subscribe to WMI events. Use a more specific derivative of this class, such as the WqlEventQuery class, whenever you can.

ManagementQuery

Abstract base class from which other query classes are derived.

ObjectQuery

Query for WMI objects that can return instances or classes.

RelatedObjectQuery

A WQL ASSOCIATORS OF query that can be used for both instances and schema queries, depending on whether you request the class or a subset of instances. Used to specify the query in the constructor of the ManagementObjectSearcher class to obtain all the instances of classes that are related through association classes.

RelationshipQuery

A WQL REFERENCES OF query that can be used for both instances and schema queries, depending on whether you request the class or a subset of instances.

SelectQuery

A WQL SELECT query that can be used for both instances and schema queries, depending on whether you request the class or a subset of instances. For more information, see "SELECT Statement for Data Queries," "SELECT Statement for Event Queries," or "SELECT Statement for Schema Queries," in the Windows Management Instrumentation documentation in the MSDN Library at http://msdn.microsoft.com/library.

WqlEventQuery

A WMI event query in WQL format. Objects of this class or its derivatives are used in the ManagementEventWatcher class to subscribe to WMI events.

WqlEventQuery

A WMI query in WQL format. Objects of this class or its derivatives are used to specify a query in the constructor of the ManagementObjectSearcher class. Use more concise derivatives of this class, such as the SelectQuery class, when possible for more specificity.

The following diagram shows the inheritance of WMI query classes in the .NET Framework class library. WMI in .NET Framework has both object and event query classes.

Class hierarchy for query classes

Query Classes and WQL Queries

The following two code examples perform equivalent queries.

This code example uses the SelectQuery class to formulate a WQL query.

Imports System
Imports System.Management

Public Class Query_SelectQuery

    Public Overloads Shared Function _
        Main(ByVal args() As String) As Integer

        Dim selectQuery As SelectQuery
        selectQuery = New SelectQuery("Win32_LogicalDisk")
        Dim searcher As ManagementObjectSearcher
        searcher = New ManagementObjectSearcher(selectQuery)

        Dim disk As ManagementObject
        For Each disk In searcher.Get()
            Console.WriteLine(disk.ToString())
        Next
        Console.ReadLine()
        Return 0

    End Function

End Class

This code example uses the original WQL form of a SELECT query.

Imports System
Imports System.Management

Public Class Query_Select_FullString

    Public Overloads Shared Function _
        Main(ByVal args() As String) As Integer

        Dim wqlQuery As WqlObjectQuery
        wqlQuery = New WqlObjectQuery( _
            "SELECT * FROM Win32_LogicalDisk")

        Dim searcher As ManagementObjectSearcher
        searcher = New ManagementObjectSearcher(wqlQuery)

        Dim disk As ManagementObject
        For Each disk In searcher.Get()
            Console.WriteLine(disk.ToString())
        Next

        Console.ReadLine()
        Return 0

    End Function
End Class

Show: