BLOG.CSHARPHELPER.COM: Use WMI to get the system's board serial numbers and CPU IDs in C#
Use WMI to get the system's board serial numbers and CPU IDs in C#
WMI (Windows Management Instrumentation) lets you use SQL-like statements to ask the computer about itself. This example uses it to get:
The serial number(s) for the system's mother board(s)
The ID(s) for the system's CPU(s)
The GetBoardSerialNumbers function shown in the following code returns a list containing the mother board serial numbers.
// Use WMI to return the system's base board serial numbers. private List GetBoardSerialNumbers() { List results = new List();
string query = "SELECT * FROM Win32_BaseBoard"; ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); foreach (ManagementObject info in searcher.Get()) { results.Add(info.GetPropertyValue("SerialNumber").ToString()); }
return results; }
The code uses the WMI query "SELECT * FROM Win32_BaseBoard" to get information about the system's mother boards (base boards). The code loops through the resulting collection of Win32_BaseBoard ManagementObjects and adds their SerialNumber values to the result list.
The GetCpuIds function shown in the following code returns a list containing the system's CPU IDs.
// Use WMI to return the CPUs' IDs. private List GetCpuIds() { List results = new List();
string query = "Select * FROM Win32_Processor"; ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); foreach (ManagementObject info in searcher.Get()) { results.Add(info.GetPropertyValue("ProcessorId").ToString()); }
return results; }
The code uses the WMI query "SELECT * FROM Win32_Processor" to get information about the system's processors. The code loops through the resulting collection of Win32_Processor ManagementObjects and adds their ProcessorId values to the result list.
The following code shows how the main program displays the results in ListBoxes.
// Display the mother board serial numbers and the CPU IDs. private void Form1_Load(object sender, EventArgs e) { lstBoardSerialNumbers.DataSource = GetBoardSerialNumbers(); lstCpuIds.DataSource = GetCpuIds(); }
Comments