Easily obtain system information (1) -wmi art in .NET
MONTAQUE
Affirmation: 1. Personal experience, for reference 2, when reprint, please keep the original.
Overview:
I don't know if you have this experience? Sometimes in order to obtain a little bit of information, such as considering the version number of the operating system, or the resolution of the current screen. In fact, the end is a property value for reading an operating system, and then seeing our programs in the dense Win32 API declaration, call, code readability and maintenance. When it comes to .NET, Microsoft provides a more rich class, and there are many ways to call the API before. Net easily and easy to implement. Today, it is briefly introduced to how to obtain information from the WMI (Windows Management Specification) in .NET.
Main ideas:
For example, an example of obtaining an operating system sharing directory and getting a motherboard number, describing how to use the class get the system below to obtain the system:
text:
WMI (Windows Management Specification: Windows Management Instrument) is an implementation of Microsoft web-based enterprise management (WBEM) and is also a standard system management interface. WMI earliest appears on the Microsoft Windows 2000 system, but it can also be installed on Windows NT 4 and Windows 9x computers. WMI is a powerful tool for easy access to system information.
In .NET, there is a system.management name space (the system is not referenced by default, we can add a reference manually), by the following Class's operation, you can query the information of the system software and hardware, first look at a simple example:
Imports System.Management Dim Searcher As New ManagementObjectSearcher ("SELECT * WIN32_SHARE")
Dim Share As ManagementObject
For Each Share in Searcher.get ()
Messagebox.show (Share.getText (TextFormat.mof))
Next Share
The result of the run is to list all the current shared directories of all systems, and description, etc.
Analyze the above code, you can see a few points:
1. It seems to be a database operation, a bit like a SQL statement. In fact, SQL operation, this statement is formed into WQL (WMI Query Language), in fact, a subset of standard SQL plus the WMI extension.
2, WQL is a read-only query language, we can only query the response data, can not update Update, Insert and other updates
3, the code is very simple, easy to understand
4, we use a MOF (managed object format) display.
Example 2: Get information about the current motherboard
The above example is a software information. Let's take an example of obtaining hardware information, get the serial number and manufacturer of the motherboard:
DIM Searcher As New ManagementObjectSearcher ("SELECT * WIN32_BASEBOARD")
Dim Share As ManagementObject
For Each Share in Searcher.get ()
Debug.writeline ("Motherboard Manufacturer:" & Share ("Manufacturer"))
Debug.WriteLine ("Model:" & Share ("Product")))
Debug.writeline ("Serial Number:" & Share ("SerialNumber")))
Next Share
Summary and add:
The WMI class is also a hierarchical, which can be referred to the WMI in MSDN; when the .NET platform is developed, it is best to see some introductions about .NET's new characteristics, this can greatly improve the development efficiency of code and operational efficiency .