How to retrieve the motherboard information with C# in WinForms

How to retrieve the motherboard information with C# in WinForms

In many applications like Speccy, you can see detailed information about different components in your CPU. If you are experimenting with C# and you want to retrieve information about the motherboard, there's a pretty easy way to do it using the WMI classes of Windows.

In this article, we'll share with you a very simple way to retrieve detailed information about the motherboard.

1. Add reference to the System.Management class

In order to obtain information about the motherboard with C# in WinForms, you will need to have access to the System Management class:

using System.Management;

However, in some Visual Studio versions (specifically in 2010 and later) you'll need to add the reference (.DLL) in your project manually. To do that follow these steps:

  1. Right Click on Project, Add References

  2. Select the Assemblies (framework) Tab and Search for System.Management and finally add the reference and click OK.

Add System.Management reference visual studio manually

We need to add the System.Management to create queries in WMI Classes. Read more about retrieving WMI Classes in .NET in msdn here.

By the other side, remember that all the implementations that uses the ManagementObjectSearcher class to obtain system information, whose properties values are integer values (0-100) and those values aren't related to the property name (i.e with the Video_Controller GPU class in the Architecture property that returns a value between 0 and 9) and you expect a very specific value (like x86 or x64), then is probably that you're passing by some information! Please read the documentation of the class in the Microsoft developer network website (providen in every part of the article respectively) to get a detailed description of every property.

For example, as said before the Architecture property returns an integer (which doesn't make sense for you as value), however this value specifies the index (not the value itself) of the following table:

Index Value
0 x86
1 MIPS
2 Alpha
3 PowerPC
5 ARM
6 ia64 (Itanium-based systems)
9 x64

So it's up to you how to render the information in your application according to the information providen in the msdn website.

2. Create the static MotherboardInfo class

As next step, now that you have access to the management class of WMI, you can register the new class (create a new class MotherboardInfo.cs) that we will use to obtain the information of the motherboard. This class is static, so you will be able to obtain some properties without needing to instantiate it:

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

static public class MotherboardInfo
{
    private static ManagementObjectSearcher baseboardSearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
    private static ManagementObjectSearcher motherboardSearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_MotherboardDevice");

    static public string Availability
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in motherboardSearcher.Get())
                {
                    return GetAvailability(int.Parse(queryObj["Availability"].ToString()));
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public bool HostingBoard
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in baseboardSearcher.Get())
                {
                    if (queryObj["HostingBoard"].ToString() == "True")
                        return true;
                    else
                        return false;
                }
                return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }

    static public string InstallDate
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in baseboardSearcher.Get())
                {
                    return ConvertToDateTime(queryObj["InstallDate"].ToString());
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public string Manufacturer
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in baseboardSearcher.Get())
                {
                    return queryObj["Manufacturer"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public string Model
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in baseboardSearcher.Get())
                {
                    return queryObj["Model"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public string PartNumber
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in baseboardSearcher.Get())
                {
                    return queryObj["PartNumber"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public string PNPDeviceID
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in motherboardSearcher.Get())
                {
                    return queryObj["PNPDeviceID"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public string PrimaryBusType
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in motherboardSearcher.Get())
                {
                    return queryObj["PrimaryBusType"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public string Product
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in baseboardSearcher.Get())
                {
                    return queryObj["Product"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public bool Removable
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in baseboardSearcher.Get())
                {
                    if (queryObj["Removable"].ToString() == "True")
                        return true;
                    else
                        return false;
                }
                return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }

    static public bool Replaceable
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in baseboardSearcher.Get())
                {
                    if (queryObj["Replaceable"].ToString() == "True")
                        return true;
                    else
                        return false;
                }
                return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }

    static public string RevisionNumber
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in motherboardSearcher.Get())
                {
                    return queryObj["RevisionNumber"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public string SecondaryBusType
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in motherboardSearcher.Get())
                {
                    return queryObj["SecondaryBusType"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public string SerialNumber
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in baseboardSearcher.Get())
                {
                    return queryObj["SerialNumber"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public string Status
    {
        get
        {
            try
            {
                foreach (ManagementObject querObj in baseboardSearcher.Get())
                {
                    return querObj["Status"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public string SystemName
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in motherboardSearcher.Get())
                {
                    return queryObj["SystemName"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    static public string Version
    {
        get
        {
            try
            {
                foreach (ManagementObject queryObj in baseboardSearcher.Get())
                {
                    return queryObj["Version"].ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                return "";
            }
        }
    }

    private static string GetAvailability(int availability)
    {
        switch (availability)
        {
            case 1: return "Other";
            case 2: return "Unknown";
            case 3: return "Running or Full Power";
            case 4: return "Warning";
            case 5: return "In Test";
            case 6: return "Not Applicable";
            case 7: return "Power Off";
            case 8: return "Off Line";
            case 9: return "Off Duty";
            case 10: return "Degraded";
            case 11: return "Not Installed";
            case 12: return "Install Error";
            case 13: return "Power Save - Unknown";
            case 14: return "Power Save - Low Power Mode";
            case 15: return "Power Save - Standby";
            case 16: return "Power Cycle";
            case 17: return "Power Save - Warning";
            default: return "Unknown";
        }
    }

    private static string ConvertToDateTime(string unconvertedTime)
    {
        string convertedTime = "";
        int year = int.Parse(unconvertedTime.Substring(0, 4));
        int month = int.Parse(unconvertedTime.Substring(4, 2));
        int date = int.Parse(unconvertedTime.Substring(6, 2));
        int hours = int.Parse(unconvertedTime.Substring(8, 2));
        int minutes = int.Parse(unconvertedTime.Substring(10, 2));
        int seconds = int.Parse(unconvertedTime.Substring(12, 2));
        string meridian = "AM";
        if (hours > 12)
        {
            hours -= 12;
            meridian = "PM";
        }
        convertedTime = date.ToString() + "/" + month.ToString() + "/" + year.ToString() + " " +
        hours.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString() + " " + meridian;
        return convertedTime;
    }
}

3. Using the class to obtain information

After registering the class, you will be able to obtain the information just casting statically the MotherboardInfo class and accesing the desired property, for example one of the most wanted features is to know the model of the motherboard in order to search for drivers:

Console.WriteLine("Motherboard Properties:");
Console.WriteLine("-----------------------------------------------------------------------------");
Console.WriteLine("-----------------------------------------------------------------------------");
Console.WriteLine("Availability: " + MotherboardInfo.Availability);
Console.WriteLine("HostingBoard: " + MotherboardInfo.HostingBoard);
Console.WriteLine("InstallDate: " + MotherboardInfo.InstallDate);
Console.WriteLine("Manufacturer: " + MotherboardInfo.Manufacturer);
Console.WriteLine("Model: " + MotherboardInfo.Model);
Console.WriteLine("PartNumber: " + MotherboardInfo.PartNumber);
Console.WriteLine("PNPDeviceID: " + MotherboardInfo.PNPDeviceID);
Console.WriteLine("PrimaryBusType: " + MotherboardInfo.PrimaryBusType);
Console.WriteLine("Product: " + MotherboardInfo.Product);
Console.WriteLine("Removable: " + MotherboardInfo.Removable);
Console.WriteLine("Replaceable: " + MotherboardInfo.Replaceable);
Console.WriteLine("RevisionNumber: " + MotherboardInfo.RevisionNumber);
Console.WriteLine("SecondaryBusType: " + MotherboardInfo.SecondaryBusType);
Console.WriteLine("SerialNumber: " + MotherboardInfo.SerialNumber);
Console.WriteLine("Status: " + MotherboardInfo.Status);
Console.WriteLine("SystemName: " + MotherboardInfo.SystemName);
Console.WriteLine("Version: " + MotherboardInfo.Version);

The output data will vary according to the specs of your computer, but will be similar to the following output:

Motherboard Properties:
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
Availability: Running or Full Power
HostingBoard: True
InstallDate: 
Manufacturer: MSI
Model: 
PartNumber: 
PNPDeviceID: 
PrimaryBusType: PCI
Product: H110M PRO-VD PLUS (MS-7A15)
Removable: False
Replaceable: True
RevisionNumber: 
SecondaryBusType: ISA
SerialNumber: GB16464640
Status: OK
SystemName: DESKTOP-N36LKRA
Version: 1.0

Happy coding !

This could interest you

Become a more social person