ManagementClass.GetInstances Method

Definition

Returns the collection of all instances of the class.

Overloads

GetInstances()

Returns the collection of all instances of the class.

GetInstances(EnumerationOptions)

Returns the collection of all instances of the class using the specified options.

GetInstances(ManagementOperationObserver)

Returns the collection of all instances of the class, asynchronously.

GetInstances(ManagementOperationObserver, EnumerationOptions)

Returns the collection of all instances of the class, asynchronously, using the specified options.

Remarks

.NET Framework Security

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

GetInstances()

Returns the collection of all instances of the class.

public:
 System::Management::ManagementObjectCollection ^ GetInstances();
public System.Management.ManagementObjectCollection GetInstances ();
member this.GetInstances : unit -> System.Management.ManagementObjectCollection
Public Function GetInstances () As ManagementObjectCollection

Returns

A collection of the ManagementObject objects representing the instances of the class.

Examples

The following example shows how to initialize a ManagementClass variable with a ManagementClass constructor and then get all the instances of a WMI class.

using System;
using System.Management;

public class Sample
{
    public static void Main()
    {
        ManagementClass c = new ManagementClass("Win32_Process");
        foreach (ManagementObject o in c.GetInstances())
            Console.WriteLine(
                "Next instance of Win32_Process : {0}", o["Name"]);
    }
}
Imports System.Management

Class Sample

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

        Dim c As New ManagementClass("Win32_Process")
        Dim o As ManagementObject
        For Each o In c.GetInstances()
            Console.WriteLine( _
                "Next instance of Win32_Process : {0}", o("Name"))
        Next o

    End Function
End Class

Remarks

.NET Framework Security

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

Applies to

GetInstances(EnumerationOptions)

Returns the collection of all instances of the class using the specified options.

public:
 System::Management::ManagementObjectCollection ^ GetInstances(System::Management::EnumerationOptions ^ options);
public System.Management.ManagementObjectCollection GetInstances (System.Management.EnumerationOptions options);
member this.GetInstances : System.Management.EnumerationOptions -> System.Management.ManagementObjectCollection
Public Function GetInstances (options As EnumerationOptions) As ManagementObjectCollection

Parameters

options
EnumerationOptions

The additional operation options.

Returns

A collection of the ManagementObject objects representing the instances of the class, according to the specified options.

Examples

The following example shows how to initialize a ManagementClass variable with a ManagementClass constructor and then get all the instances of a WMI class and its subclasses.

using System;
using System.Management;

public class Sample
{
    public static void Main()
    {
        EnumerationOptions opt = new EnumerationOptions();
        // Will enumerate instances of the given class and any subclasses.
        opt.EnumerateDeep = true;
        ManagementClass c = new ManagementClass("CIM_Service");
        foreach (ManagementObject o in c.GetInstances(opt))
            Console.WriteLine(o["Name"]);
    }
}
Imports System.Management

Class Sample

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

        Dim opt As New EnumerationOptions
        ' Will enumerate instances of the given class and any subclasses.
        opt.EnumerateDeep = True
        Dim mngmtClass As New ManagementClass("CIM_Service")
        Dim o As ManagementObject
        For Each o In mngmtClass.GetInstances(opt)
            Console.WriteLine(o("Name"))
        Next o

    End Function
End Class

Remarks

.NET Framework Security

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

Applies to

GetInstances(ManagementOperationObserver)

Returns the collection of all instances of the class, asynchronously.

public:
 void GetInstances(System::Management::ManagementOperationObserver ^ watcher);
public void GetInstances (System.Management.ManagementOperationObserver watcher);
member this.GetInstances : System.Management.ManagementOperationObserver -> unit
Public Sub GetInstances (watcher As ManagementOperationObserver)

Parameters

watcher
ManagementOperationObserver

The object to handle the asynchronous operation's progress.

Examples

The following example shows how to initialize a ManagementClass variable with a ManagementClass constructor and then get all the instances of a WMI class asynchronously.

using System;
using System.Management;

public class AsyncGetExample
{
    public AsyncGetExample()
    {
        ManagementClass c =
            new ManagementClass("Win32_Process");
        ManagementOperationObserver ob =
            new ManagementOperationObserver();
        ob.ObjectReady += new ObjectReadyEventHandler(NewObject);
        ob.Completed += new CompletedEventHandler(Done);

        c.GetInstances(ob);

        while (!Completed)
            System.Threading.Thread.Sleep (1000);

        // Here you can use the object
    }

    private bool completed = false;

    private void NewObject(object sender,
        ObjectReadyEventArgs e)
    {
        Console.WriteLine("New result arrived: {0}",
            ((ManagementObject)(e.NewObject))["Name"]);
    }

    private void Done(object sender,
        CompletedEventArgs e)
    {
        Console.WriteLine("async Get completed !");
        completed = true;
    }

    private bool Completed
    {
        get
        {
            return completed;
        }
    }

    public static void Main()
    {
        AsyncGetExample asyncGet = new
            AsyncGetExample();

        return;
    }
}
Imports System.Management

Public Class AsyncGetExample

    Public Sub New()

        Dim c As New ManagementClass("Win32_Process")
        Dim ob As New ManagementOperationObserver
        AddHandler ob.ObjectReady, AddressOf Me.NewObject
        AddHandler ob.Completed, AddressOf Me.Done

        c.GetInstances(ob)

        While Not Me.Completed
            System.Threading.Thread.Sleep(1000)
        End While

        'Here you can use the object

    End Sub

    Private finished As Boolean = False

    Private Sub NewObject(ByVal sender As Object, _
    ByVal e As ObjectReadyEventArgs)
        Console.WriteLine("New result arrived: {0}", _
         e.NewObject("Name"))
    End Sub

    Private Sub Done(ByVal sender As Object, _
    ByVal e As CompletedEventArgs)
        Console.WriteLine("async Get completed !")
        finished = True
    End Sub

    Private ReadOnly Property Completed() As Boolean
        Get
            Return finished
        End Get
    End Property


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

        Dim asyncGet As New AsyncGetExample

        Return 0

    End Function

End Class

Remarks

.NET Framework Security

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

Applies to

GetInstances(ManagementOperationObserver, EnumerationOptions)

Returns the collection of all instances of the class, asynchronously, using the specified options.

public:
 void GetInstances(System::Management::ManagementOperationObserver ^ watcher, System::Management::EnumerationOptions ^ options);
public void GetInstances (System.Management.ManagementOperationObserver watcher, System.Management.EnumerationOptions options);
member this.GetInstances : System.Management.ManagementOperationObserver * System.Management.EnumerationOptions -> unit
Public Sub GetInstances (watcher As ManagementOperationObserver, options As EnumerationOptions)

Parameters

watcher
ManagementOperationObserver

The object to handle the asynchronous operation's progress.

options
EnumerationOptions

The specified additional options for getting the instances.

Remarks

.NET Framework Security

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

Applies to