ManagementClass.GetInstances 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回類別所有執行個體的集合。
多載
GetInstances() |
傳回類別所有執行個體的集合。 |
GetInstances(EnumerationOptions) |
使用指定的選項,傳回類別所有執行個體的集合。 |
GetInstances(ManagementOperationObserver) |
以非同步方式傳回類別所有執行個體的集合。 |
GetInstances(ManagementOperationObserver, EnumerationOptions) |
使用指定的選項,以非同步方式傳回類別所有執行個體的集合。 |
備註
.NET Framework 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。
GetInstances()
傳回類別所有執行個體的集合。
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)
使用指定的選項,傳回類別所有執行個體的集合。
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)
以非同步方式傳回類別所有執行個體的集合。
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)
使用指定的選項,以非同步方式傳回類別所有執行個體的集合。
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 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。