Using C # to manipulate the WMI Guide _c# tutorial

Source: Internet
Author: User

1 What is WMI?

Windows Management Instrumentation (WMI) is a scalable system management architecture that employs a unified, standards-based, and extensible object-oriented interface. It provides a standard method of interacting with system administrator information and the underlying WMI API, used primarily by system management application developers and system administrators to access and operating system management information, and can be used to generate tools for organizing and managing system information, enabling system administrators to monitor system activities more closely.

WMI provides a rich set of system management services built into Microsoft Windows operating systems that provide a full range of management capabilities in systems with a large number of applications, services, and devices. It allows application developers to use simple, consistent mechanisms to query information on any computer in the enterprise, or to configure the system.

The amount of information available through the WMI interface is staggering, including hardware settings, status information, drive configuration, BIOS information, application settings, event logging information, and more. WMI obtains information through a set of APIs, but it represents a function that obtains information through a simple, industrial-standard object management pattern. This allows application developers to not have to learn the specifics of every API in Windows.

The. NET Framework SDK provides comprehensive support for WMI, and the. NET Framework SDK provides a dedicated namespace "System.Management" for Visual C # to manipulate WMI. The namespace "System.Management" provides a large number of classes, interfaces, and enumerations that are used to handle WMI-related. Before using WMI, you must add a reference to System.Management.dll in your project, and then declare

using System.Management;

2 using WMI to connect to a remote computer

The system administrator can query the status and information of the remote computer through the machine name (or IP address), username and password of the target machine, and perform some administrative work using the script. Using WMI to connect to a remote computer is convenient, and you can refer to the following format

ManagementObjectSearcher query;   
Managementobjectcollection querycollection;
System.Management.ObjectQuery OQ;

string machinename = "110.119.110.1"; The IP address of the connected target machine or the machine name
Co. Username = "YourName";         The user name Co required for the connection
. Password = "YourPassword";      Connection required password
string connectstring = "SELECT * from Win32_pnpsigneddriver";  Query string

System.Management.ManagementScope ms = new System.Management.ManagementScope ("\\\\" + machinename + "\ Root\\cimv2 ", CO); 
OQ = new System.Management.ObjectQuery (connectstring);
query = new ManagementObjectSearcher (MS, OQ);
querycollection = query. Get ();

With the IP address, the username and password Create a connection that can query the Win32_pnpsigneddrvier class (for more information about this class, please query MSDN) for all information. Now we can remotely get all the PnP-driven information on the target machine. Is it convenient? After getting all the information, we can get the attributes we need in the following two ways

foreach (ManagementObject mo in querycollection)
{
 //string HardwareID  = mo["HardwareID"]); Get the value of the property directly according to the property name
 
 //Traverse all the properties, get the value of all the properties
 propertydatacollection searcherproperties = mo. Properties;
 foreach (Propertydata sp in searcherproperties)
 {
 Console.WriteLine ("Name = {0, -20}, Value = {1, -20}", sp. Name,sp. Value);
 }

3 using WMI to connect to the local computer

It is also convenient to use WMI to connect to the local computer, so we just need to modify the above code a little bit.

string machinename = "localhost";
Co. Username = "";
Co. Password = "";

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

String connectstring = "SELECT * from Win32_pnpsigneddriver";

SelectQuery selectquery = new SelectQuery (connectstring);
ManagementObjectSearcher searcher = new ManagementObjectSearcher (selectquery);

foreach (ManagementObject mo in searcher. Get ())
{
 propertydatacollection searcherproperties = mo. Properties;
 foreach (Propertydata sp in searcherproperties)
 {
 Console.WriteLine (sp. Name + "" + sp. Value);
 }

4 Format of query statements

The connection string above connectstring actually has many different formats. For WMI, the information needed to use WQL queries. WQL inherits some of the syntax of SQL. But it's not that all SQL statements can be used for WQL. There are several common formats.

(1) Querying all properties of all objects in a class in the system
connectstring = "SELECT * FROM Win32_pnpsigneddriver"

(2) Querying the HardwareID properties of all objects in a class
connectstring = "Select HardwareID from Win32_pnpsigneddriver"

(3) Querying the HardwareID and DriverVersion properties of all objects in a class
connectstring = "Select HardwareID, driverversion from Win32_pnpsigneddriver"

(4) Query the HardwareID and DriverVersion properties of all objects in the 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 properties of all objects in the class, and the HardwareID of the object must begin with monitor
connectstring = "Select HardwareID, driverversion from Win32_pnpsigneddriver WHERE hardwareid like ' monitor% '"

(6) Query the HardwareID and DriverVersion properties of all objects in the class, and the HardwareID of the object must begin with monitor, ending with a ibm254d, with only one arbitrary character
connectstring = "Select HardwareID, driverversion from Win32_pnpsigneddriver WHERE hardwareid like ' monitor_ibm254d '"

(7) Query the HardwareID and DriverVersion properties of all objects in the class, and the HardwareID of the object must begin with monitor, ending with a ibm254d and having only one arbitrary character between them. And to meet the InfName must be Oem18.inf
connectstring = "Select HardwareID, driverversion from Win32_pnpsigneddriver WHERE hardwareid like ' monitor_ibm254d ' and I Nfname= ' Oem18.inf '

(8) Query the HardwareID and DriverVersion properties of all objects in the class, and the HardwareID of the object must begin with monitor, ending with a ibm254d and having only one arbitrary character between them. Or meet InfName is Oem18.inf
connectstring = "Select HardwareID, driverversion from Win32_pnpsigneddriver WHERE hardwareid like ' monitor_ibm254d ' OR in Fname= ' 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

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.