ManagementObject.Get 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
繫結至管理物件。
多載
Get() |
將 WMI 類別資訊繫結至管理物件。 |
Get(ManagementOperationObserver) |
非同步繫結至管理物件。 |
備註
.NET Framework 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。
Get()
將 WMI 類別資訊繫結至管理物件。
public:
void Get();
public void Get ();
member this.Get : unit -> unit
Public Sub Get ()
範例
下列範例會 Get 呼叫 方法來取得 類別的 ManagementObject 實例。
using System;
using System.Management;
class Sample
{
public static int Main(string[] args)
{
ManagementObject o =
new ManagementObject("MyClass.Name='abc'");
//this causes an implicit Get().
string s = o["Name"].ToString();
Console.WriteLine(s);
//or :
ManagementObject mObj =
new ManagementObject("MyClass.Name= 'abc'");
mObj.Get(); //explicitly
// Now it is faster because the object
// has already been retrieved.
string property = mObj["Name"].ToString();
Console.WriteLine(property);
return 0;
}
}
Imports System.Management
Class Sample_ManagementClass
Public Overloads Shared Function Main( _
ByVal args() As String) As Integer
Dim o As New ManagementObject( _
"MyClass.Name=""abc""")
'this causes an implicit Get().
Dim s As String = o("SomeProperty")
Console.WriteLine(s)
'or :
Dim mObj As New ManagementObject("MyClass.Name= ""abc""")
mObj.Get() 'explicitly
' Now it is faster because the object
' has already been retrieved.
Dim p As String = mObj("SomeProperty")
Console.WriteLine(p)
Return 0
End Function
End Class
備註
第一次嘗試取得或設定 WMI 物件的資訊時,會隱含叫用 方法。 您也可以在使用者自行決定時明確叫用,以更妥善地控制擷取的時間和方式。
.NET Framework 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。
適用於
Get(ManagementOperationObserver)
非同步繫結至管理物件。
public:
void Get(System::Management::ManagementOperationObserver ^ watcher);
public void Get (System.Management.ManagementOperationObserver watcher);
member this.Get : System.Management.ManagementOperationObserver -> unit
Public Sub Get (watcher As ManagementOperationObserver)
參數
- watcher
- ManagementOperationObserver
要將作業結果當成事件接收的物件。
範例
下列範例會 Get 呼叫 方法,以異步方式取得 類別的 ManagementObject 實例。
using System;
using System.Management;
public class AsyncGetExample
{
public AsyncGetExample()
{
ManagementObject o =
new ManagementObject(
"Win32_Process.Name='notepad.exe'");
// Set up handlers for asynchronous get
ManagementOperationObserver ob =
new ManagementOperationObserver();
ob.Completed += new
CompletedEventHandler(this.Done);
// Get the object asynchronously
o.Get(ob);
// Wait until operation is completed
while (!this.Completed)
System.Threading.Thread.Sleep (1000);
// Here you can use the object
}
private bool completed = false;
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 example =
new AsyncGetExample();
}
}
Imports System.Management
Class AsyncGetExample
Public Sub New()
Dim o As New ManagementObject( _
"Win32_Process.Name=""notepad.exe""")
'Set up handlers for asynchronous get
Dim ob As New ManagementOperationObserver
AddHandler ob.Completed, AddressOf Me.Done
'Get the object asynchronously
o.Get(ob)
'Wait until operation is completed
While Not Me.Completed
System.Threading.Thread.Sleep(1000)
End While
'Here you can use the object
End Sub
Private _completed As Boolean = False
Private Sub Done(ByVal sender As Object, _
ByVal e As CompletedEventArgs)
Console.WriteLine("async Get completed !")
_completed = True
End Sub
Private ReadOnly Property Completed() As Boolean
Get
Return _completed
End Get
End Property
Public Overloads Shared Function Main( _
ByVal args() As String) As Integer
Dim example As New AsyncGetExample
Return 0
End Function
End Class
備註
方法會發出要求以取得 物件,然後立即傳回 。 接著,作業的結果會透過在提供的監看員對象上引發的事件來傳遞。
.NET Framework 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。