Get the CPU serial number code
string cpuInfo = "";//cpu serial number
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = ();
foreach(ManagementObject mo in moc)
{
cpuInfo = ["ProcessorId"].();
(cpuInfo);
();
}
Obtain the network card hardware address
using ;
...
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = ();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
("MAC address\t{0}", mo["MacAddress"].ToString());
();
}
}
Get the hard disk ID
String HDid;
ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = ();
foreach(ManagementObject mo in moc)
{
HDid = (string)["Model"].value;
(HDid );
}
16. Easily obtain system information in .NET (1) - WMI Chapter
Montaque
Statement:
1. My personal experience is for reference only
2. Please keep the original when reprinting.
Overview:
I wonder if you have this experience? Sometimes in order to obtain a little information about the system, for example, consider the version number of the operating system, or the resolution of the current screen. In fact, it is just reading a property value in a certain aspect of the operating system, and then you will see the dense Win32 API declarations, calls, and code readability and maintenance of our program are self-evident. By .NET, Microsoft provides more rich classes, and there are many methods that used to call APIs that can be easily implemented in .NET. Today I briefly introduce how to obtain information through communication with WMI (Windows Management Specification) in .NET.
Main ideas:
Take an example of obtaining the operating system shared directory and obtaining the motherboard number, and introduce how to use the following classes to obtain system-related information:
Text:
WMI (Windows Management Specification: Windows Management Instrumentation) is an implementation of Microsoft's Web-based Enterprise Management (WBEM) and is also a standard-based system management interface. WMI first appeared on Microsoft Windows 2000 systems, but it can also be installed on Windows NT 4 and Windows 9x computers. WMI is a powerful tool for easily obtaining system information.
In .NET, there is a namespace (the system has no references by default, we can add references manually). Through the following Class operations, you can query the system software and hardware information. Let’s take a look at a simple example:
Imports
Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_share")
Dim share As ManagementObject
For Each share In ()
(())
Next share
The result of the operation is to list all the currently shared directories, descriptions, etc. of the system.
After analyzing the above code, you can see the following points:
1. It seems to be performing database operations, a bit like SQL statements. In fact, it is SQL operation. This statement is called WQL (WMI Query Language), which is actually a subset of standard SQL with WMI extensions.
2. WQL is a read-only query language. We can only query the response data, and cannot use UPDATE, INSERT and other update operations.
3. The code is simple and 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 a look at an example of obtaining hardware information to obtain the serial number of the motherboard and the manufacturer:
Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard")
Dim share As ManagementObject
For Each share In ()
("Motherboard manufacturer:" & share("Manufacturer"))
("Model:" & share("Product"))
("Serial Number:" & share("SerialNumber"))
Next share
Summary and supplement:
The WMI class is also hierarchical. For details, you can refer to WMI in msdn; when moving to the development of .NET platform, it is best to read more introductions to the new features of .NET, so as to greatly improve the development efficiency and operation efficiency of the code.