How to use C # WMI

Source: Internet
Author: User

1. What is WMI?

Windows Management Instrumentation (Wmi) is a scalable system management structure that uses a unified, standard-based, and scalable object-oriented interface. It provides standard methods for interacting with System Administrator information and basic WMI APIs, primarily by system management applications.ProgramDevelopers and system administrators can access and manage operating system information. They can be used to generate tools for organizing and managing system information so that system administrators can monitor system activities more closely.

WMI provides a set of rich System Management Services built in Microsoft Windows operating systems, and provides comprehensive management functions in systems with a large number of applications, services and devices. It allows application developers to use a simple and consistent mechanism to query information on any computer in an enterprise or perform system configuration.

The amount of information that can be obtained through the WMI interface is astonishing, including hardware settings, status information, drive configuration, BIOS information, application settings, event record information, and others. WMI obtains information through a set of APIS, but it represents a function that obtains information through a simple, industrial standard object management mode. This makes it unnecessary for application developers to learn the details of every API in windows.

The. NET Framework SDK provides comprehensive support for WMI. the. NET Framework SDK provides a special namespace "system. Management" for Visual C # To operate WMI ". The namespace "system. Management" provides a large number of WMI-related classes, interfaces, and enumeration. Before Using WMI, you must add a reference to system. Management. dll in the project, and then declare

Using System. Management;

2. Use WMI to connect to a remote computer

The system administrator can query the status and information of the remote computer by the machine name (or IP address), user name, and password of the target machine, and execute some management tasks by using scripts. It is very convenient to use WMI to connect to a remote computer. You can refer to the following format:

Managementobjectsearcher query;
Managementobjectcollection querycollection;
System. Management. objectquery OQ;

String Machinename =   " 110.119.110.1 " ; // IP address or machine name of the connected target machine
CO. Username =   " Yourname " ; // Username required for connection
CO. Password =   " Yourpassword " ; // Password required for connection
String Connectstring =   " Select * From win32_pnpsigneddriver " ; // Query string

MS system. Management. managementscope =   New System. Management. managementscope ( " \\\\ "   + Machinename +   " \ Root \ cimv2 " , CO );
OQ =   New System. Management. objectquery (connectstring );
Query =   New Managementobjectsearcher (MS, OQ );
Querycollection = Query. Get ();

Through the IP address, the user name and password establish a connection that can query all the information of the win32_pnpsigneddrvier class (for details about this class, please query msdn. Now we can remotely obtain information about all the PNP drivers on the target machine. Is it convenient? After obtaining all the information, we can use the following two methods to obtain the required attributes.

Foreach (Managementobject Mo In Querycollection)
{
  // String hardwareid = Mo ["hardwareid"]); // Obtain the attribute value based on the attribute name.
 
  // Traverse all attributes to obtain the values of all attributes.
Propertydatacollection searcherproperties = Mo. properties;
  Foreach (Propertydata sp In Searcherproperties)
{
Console. writeline ( " Name = {0,-20}, value = {1,-20} " , Sp. Name, sp. value );
}
}

3. Use WMI to connect to a local computer

Using WMI to connect to a local computer is also very convenient, we just need to modify the aboveCodeYou can.

String Machinename =   " Localhost " ;
CO. Username =   "" ;
CO. Password =   "" ;

You can also use the query statement directly in a relatively simple format.

String Connectstring =   " Select * From win32_pnpsigneddriver " ;

Selectquery= NewSelectquery (connectstring );
Managementobjectsearcher searcher= NewManagementobjectsearcher (selectquery );

Foreach (Managementobject Mo In Searcher. Get ())
{
Propertydatacollection searcherproperties = Mo. properties;
  Foreach (Propertydata sp In Searcherproperties)
{
Console. writeline (sp. Name +   " "   + Sp. value );
}
}

4. query statement format

The above connection string connectstring actually has many formats. For WMI, WQL is used to query the required information. WQL inherits some SQL syntax. It does not mean that all SQL statements can be used in WQL. Common formats are as follows.

(1) All attributes of all objects in the query system
Connectstring = "select * From win32_pnpsigneddriver"

(2) query the hardwareid attributes of all objects in the class.
Connectstring = "select hardwareid from win32_pnpsigneddriver"

(3) query the hardwareid and driverversion attributes of all objects in the class.
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver"

(4) The hardwareid and driverversion attributes of all objects in the query class, And the hardwareid of the object must end with ibm254d
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver where hardwareid like '% ibm254d '"

(5) query the hardwareid and driverversion attributes of all objects in the class, and the hardwareid of the object must start with monitor.
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver where hardwareid like 'Monitor % '"

(6) query the hardwareid and driverversion attributes of all objects in the class, and the hardwareid of the object must start with monitor and end with ibm254d, and there is only one arbitrary character between them.
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver where hardwareid like 'Monitor _ ibm254d '"

(7) The hardwareid and driverversion attributes of all objects in the query class, And the hardwareid of the object must start with monitor and end with ibm254d, and there must be only one arbitrary character between them. The infname must be oem18.inf.
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver where hardwareid like 'Monitor _ ibm254d 'and infname = 'oem18. inf '"

(8) query the hardwareid and driverversion attributes of all objects in the class. The hardwareid of the object must start with monitor and end with ibm254d. There is only one character between them, or the infname must be oem18.inf.
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver where hardwareid like 'Monitor _ ibm254d 'or infname = 'oem18. inf '"

5. References and tools
WMI reference: http://msdn2.microsoft.com/en-us/library/aa394572.aspx
WMI tools: http://www.microsoft.com/downloads/details.aspx? Familyid = 6430f853-1120-48db-8cc5-f2abdc3ed314 & displaylang = en
WQL query: http://www.microsoft.com/china/technet/community/scriptcenter/topics/win2003/like.mspx#EIB

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.