Title: Get the system's board serial numbers and CPU IDs in C#
This example shows how to get the system's board serial numbers and CPU IDs. WMI (Windows Management Instrumentation) lets you use SQL-like statements to ask the computer about itself.
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;
}
This method 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();
}
This code simply calls the two methods to get the board serial numbers and the CPU IDs. It then sets the ListBox controls' DataSource properties equal to the returned lists of values.
Download the example to experiment with it and to see additional details.
|