ManagementClass.GetInstances 方法

定义

返回该类的所有实例的集合。

重载

GetInstances()

返回该类的所有实例的集合。

GetInstances(EnumerationOptions)

使用指定选项返回该类的所有实例的集合。

GetInstances(ManagementOperationObserver)

异步返回包含该类的所有实例的集合。

GetInstances(ManagementOperationObserver, EnumerationOptions)

使用指定选项异步返回包含类的所有实例的集合。

注解

.NET Framework 安全性

对直接调用方的完全信任。 此成员不能由部分信任的代码使用。 有关详细信息,请参阅 使用部分受信任的代码中的库

GetInstances()

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

返回该类的所有实例的集合。

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

返回

表示该类的实例的 ManagementObject 对象的集合。

示例

以下示例演示如何使用ManagementClass构造函数初始化ManagementClass变量,然后获取 WMI 类的所有实例。

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

注解

.NET Framework 安全性

对直接调用方的完全信任。 此成员不能由部分信任的代码使用。 有关详细信息,请参阅 使用部分受信任的代码中的库

适用于

GetInstances(EnumerationOptions)

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

使用指定选项返回该类的所有实例的集合。

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

参数

options
EnumerationOptions

其他操作选项。

返回

根据指定选项表示该类的实例的 ManagementObject 对象的集合。

示例

以下示例演示如何使用ManagementClass构造函数初始化ManagementClass变量,然后获取 WMI 类及其子类的所有实例。

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

注解

.NET Framework 安全性

对直接调用方的完全信任。 此成员不能由部分信任的代码使用。 有关详细信息,请参阅 使用部分受信任的代码中的库

适用于

GetInstances(ManagementOperationObserver)

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

异步返回包含该类的所有实例的集合。

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)

参数

watcher
ManagementOperationObserver

处理异步操作进度的对象。

示例

以下示例演示如何使用ManagementClass构造函数初始化ManagementClass变量,然后异步获取 WMI 类的所有实例。

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

注解

.NET Framework 安全性

对直接调用方的完全信任。 此成员不能由部分信任的代码使用。 有关详细信息,请参阅 使用部分受信任的代码中的库

适用于

GetInstances(ManagementOperationObserver, EnumerationOptions)

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

使用指定选项异步返回包含类的所有实例的集合。

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)

参数

watcher
ManagementOperationObserver

处理异步操作进度的对象。

options
EnumerationOptions

有关获取实例的指定附加选项。

注解

.NET Framework 安全性

对直接调用方的完全信任。 此成员不能由部分信任的代码使用。 有关详细信息,请参阅 使用部分受信任的代码中的库

适用于