Using C # to manipulate WMI guidelines

Source: Internet
Author: User
1 What is WMI?

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

WMI provides a comprehensive set of system management services built into the Microsoft Windows operating system that provides 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 for querying information on any computer in the enterprise or for system configuration.

The amount of information available through the WMI interface is amazing, 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 characterizes a function that obtains information through a simple, industry-standard object management pattern. This makes it unnecessary for application developers 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 be able to manipulate WMI. A number of classes, interfaces, and enumerations related to WMI are provided in the namespace "System.Management". Before using WMI, you must add a reference to the System.Management.dll in your project, and then declare

Using System.Management;

2 Connecting a remote computer using WMI

The system administrator can query the status and information of the remote computer through the machine name (or IP address) of the target machine, user name and password, and perform some administrative work with the script. It is convenient to use WMI to connect to a remote computer, 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 or machine name of the connected target machine Co.username = "YourName";         The user name that is required to connect the Co. 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 = q Uery. Get ();

With the IP address, the username and password Create a connection that can query the win32_pnpsigneddrvier of this class (for more information on this class, see MSDN) for all information. Now we can get all the PNP driver information on the target machine remotely. Isn't it convenient? After getting all the information, we can use the following two ways to get the attributes we need

foreach (ManagementObject mo in querycollection) {//string HardwareID  = mo["HardwareID"]);//Get the value of the property directly from the property name    // Traverse all properties to get the value of all 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 very convenient to use WMI to connect to the local computer, and 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 relatively simple 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 above connection string connectstring actually has a number of formats. For WMI, use the information required for WQL queries. WQL inherits some of the syntax of SQL. However, not all SQL statements can be used in 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 property 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 start with monitor
connectstring = "Select HardwareID, driverversion from Win32_pnpsigneddriver WHERE hardwareid like ' monitor% '"

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

(7) The HardwareID and DriverVersion properties of all objects in a class are queried, and the HardwareID of the object must start with a monitor, end with ibm254d, and have only one arbitrary character, And to meet InfName must be Oem18.inf
connectstring = "Select HardwareID, driverversion from Win32_pnpsigneddriver WHERE hardwareid like ' monitor_ibm254d ' and I Nfname= ' Oem18.inf ' "

(8) The HardwareID and DriverVersion properties of all objects in a class are queried, and the HardwareID of the object must start with a monitor, end with ibm254d, and have only one arbitrary character, 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#eib

The above is the C # Operation WMI Guide, more related articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.