. Net (C #): WMI query of computers in the network domain

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.