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()

Source:
ManagementClass.cs
Source:
ManagementClass.cs
Source:
ManagementClass.cs

Returns the collection of all instances of the class.

public System.Management.ManagementObjectCollection GetInstances ();

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"]);
    }
}

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

.NET 9 (package-provided) und andere Versionen
Produkt Versionen
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

GetInstances(EnumerationOptions)

Source:
ManagementClass.cs
Source:
ManagementClass.cs
Source:
ManagementClass.cs

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

public System.Management.ManagementObjectCollection GetInstances (System.Management.EnumerationOptions options);

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"]);
    }
}

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

.NET 9 (package-provided) und andere Versionen
Produkt Versionen
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

GetInstances(ManagementOperationObserver)

Source:
ManagementClass.cs
Source:
ManagementClass.cs
Source:
ManagementClass.cs

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

public void GetInstances (System.Management.ManagementOperationObserver watcher);

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;
    }
}

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

.NET 9 (package-provided) und andere Versionen
Produkt Versionen
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

GetInstances(ManagementOperationObserver, EnumerationOptions)

Source:
ManagementClass.cs
Source:
ManagementClass.cs
Source:
ManagementClass.cs

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);

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

.NET 9 (package-provided) und andere Versionen
Produkt Versionen
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)