A wmi query and analysis tool is created.

Source: Internet
Author: User
WMI is called MicrosoftWindowsManagement Instrumentation (WMI)
According to Microsoft, the introduction is roughly as follows:
WMI is Microsoft's main management support technology for Windows. Before WMI, all Windows Graphical management tools relied on the Win32 Application Programming Interface (Application Programming Interfaces, APIs) to access and manage Windows resources. Before WMI, the only way to access Windows resources programmatically is through the Win32 API. In this case, Windows Administrators cannot use common scripting languages to automate common system management tasks, because most scripting languages cannot directly call Win32 APIs. By providing consistent models and frameworks, WMI changes this situation-through models and frameworks, all Windows resources are described and made public to the outside world. The best thing is that the system administrator can use the WMI script library to create system management scripts to manage any Windows resources exposed Through WMI!

I don't remember when I knew this WMI, but I did not take it seriously until today. Maybe it attracted me to use the SQL syntax, at least it is easy to use the system information obtained by "windows optimization master. I have been working on this small program at night. Because WMI is designed to cover all aspects of windows, I want to develop a query analyzer tool for SQL Server to debug WMI queries; it is easy to use ,. net provides a System. management. the ManagementObjectSearcher class can be used to operate WMI easily. The following is a simple example:

ManagementObjectSearcher search = new ManagementObjectSearcher ("SELECT * FROM Win32_ComputerSystem ");
SysInfoListTest. Items. Clear ();
SysInfoListTest. Items. Add ("Computer System Information ");
Foreach (ManagementObject info in search. Get ())
{
SysInfoListTest. Items. Add ("Manufacturer:" + info ["manufacturer"]. ToString ());
SysInfoListTest. Items. Add ("Model:" + info ["model"]. ToString ());
SysInfoListTest. Items. Add ("System Type:" + info ["systemtype"]. ToString ());
SysInfoListTest. Items. Add ("Total Physical Memory:" + info ["totalphysicalmemory"]. ToString ());
}

This is some basic information for accessing the computer system, such as the system's "physical memory" size;

I implemented a memory table in the program for displaying data in the dataGrid. By accessing the attributes of ManagementObject, the names of each class are displayed as columns; the following code is the core of the implementation: foreach (ManagementObject mo in mos. get ())
{
DataRow dr = dt. NewRow ();

PropertyDataCollection. PropertyDataEnumerator oEnum;
// Display the system class
// Get attributes, system class
OEnum = (mo. Properties. GetEnumerator () as PropertyDataCollection. PropertyDataEnumerator );
While (oEnum. MoveNext ())
{
PropertyData prop = (PropertyData) oEnum. Current;
If (dt. Columns. IndexOf (prop. Name) =-1)
{
Dt. Columns. Add (prop. Name );
Console. Out. WriteLine (prop. Name );
Dt. Columns [dt. Columns. Count-1]. DefaultValue = "";
}
Try
{
// This class is not instantiated, so an exception structure is required, which is null.
Dr [prop. Name] = mo [prop. Name]. ToString ();
Console. Out. WriteLine (prop. Name + "row ");
}
Catch
{
Console. Out. WriteLine (prop. Name + "row error ");
}
}
}

You can see the code:
Mo. Properties. GetEnumerator () as PropertyDataCollection. PropertyDataEnumerator
The Properties here is a non-system property set. Since it is a debugging tool, you have to list SystemProperties as well. The implementation process is the same as above.

The following figure shows the running interface:


PS: I deliberately reduced it because the picture of each post is too large.

Next time, read all accessible classes;

Click here to download the project source code with the execution File

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.