WMI supports computing a queries in the network domain. There are also multiple implementation methods in. net.
First of all, technically, whenever a WMI query is performed (even if you directly use the quick API method provided by managementobjectsearcher), there will be a connection process of WMI to the Information Officer's computer. The system. Management namespace has specific classes to meet such requirements. Managementpath represents an abstract WMI path, which can be a WQL query or a WMI namespace path (of course, "WQL can be added to the WMI namespace path ). Managementscope is the combination of the WMI path and connection options, which can represent a connection.
For example, to connect a WMI basic namespace (Root \ cimv2) named "mgen-PC" in a Windows network domain ):
// Note: Reference System. Management. dll and using system. Management;
VaR mgrpath = new managementpath (@ "\ mgen-PC \ Root \ cimv2 ");
VaR mgrscope = new managementscope (mgrpath );
// Try to connect
Mgrscope. Connect ();
// Whether the output is connected
Console. writeline (mgrscope. isconnected );
// Name of the output server (corresponding computer)
Console. writeline (mgrpath. Server );
// Output the WMI namespace
Console. writeline (mgrpath. namespacepath );
Output:
True
Mgen-PC
Root \ cimv2
With managementscope, you can connect WQL query objects to create managementobjectsearcher and finally execute WMI query:
// Note: Reference System. Management. dll and using system. Management;
VaR mgrpath = new managementpath (@ "\ mgen-PC \ Root \ cimv2 ");
VaR mgrscope = new managementscope (mgrpath );
VaR query = new objectquery ("select * From win32_process ");
// Use managementscope and objectquery to create managementobjectsearcher
VaR searcher = new managementobjectsearcher (mgrscope, query );
// Query
Foreach (managementbaseobject bobj in searcher. Get ())
Console. writeline (bobj ["name"]);
Of course, you can actually use the faster method provided by managementobjectsearcher. The two strings represent managementscope and objectquery respectively:
VaR searcher = new managementobjectsearcher (@ "\ mgen-PC \ Root \ cimv2", "select * From win32_process ");