get hardware information

Get PC Hardware Information

The Computer System Hardware category groups classes together that represent hardware related objects. This article demonstrates how to get hardware information c# in 2 different ways.

Get Hardware Information using C#

You can use the System.Management and System.Environment namespace for retrieving the hardware information of a machine using C#. Find the below code snippet for fetching Hardware Information using C#:

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

public static class HardwareInfo
{
    /// <summary>
    /// Retrieving Processor Id.
    /// </summary>
    /// <returns></returns>
    /// 
    public static String GetProcessorId()
    {

        ManagementClass mc = new ManagementClass("win32_processor");
        ManagementObjectCollection moc = mc.GetInstances();
        String Id = String.Empty;
        foreach (ManagementObject mo in moc)
        {

            Id = mo.Properties["processorID"].Value.ToString();
            break;
        }
        return Id;

    }
    /// <summary>
    /// Retrieving HDD Serial No.
    /// </summary>
    /// <returns></returns>
    public static String GetHDDSerialNo()
    {
        ManagementClass mangnmt = new ManagementClass("Win32_LogicalDisk");
        ManagementObjectCollection mcol = mangnmt.GetInstances();
        string result = "";
        foreach (ManagementObject strt in mcol)
        {
            result += Convert.ToString(strt["VolumeSerialNumber"]);
        }
        return result;
    }
    /// <summary>
    /// Retrieving System MAC Address.
    /// </summary>
    /// <returns></returns>
    public static string GetMACAddress()
    {
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc = mc.GetInstances();
        string MACAddress = String.Empty;
        foreach (ManagementObject mo in moc)
        {
            if (MACAddress == String.Empty)
            {
                if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
            }
            mo.Dispose();
        }

        MACAddress = MACAddress.Replace(":", "");
        return MACAddress;
    }
    /// <summary>
    /// Retrieving Motherboard Manufacturer.
    /// </summary>
    /// <returns></returns>
    public static string GetBoardMaker()
    {

        ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");

        foreach (ManagementObject wmi in searcher.Get())
        {
            try
            {
                return wmi.GetPropertyValue("Manufacturer").ToString();
            }

            catch { }

        }

        return "Board Maker: Unknown";

    }
    /// <summary>
    /// Retrieving Motherboard Product Id.
    /// </summary>
    /// <returns></returns>
    public static string GetBoardProductId()
    {

        ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");

        foreach (ManagementObject wmi in searcher.Get())
        {
            try
            {
                return wmi.GetPropertyValue("Product").ToString();

            }

            catch { }

        }

        return "Product: Unknown";

    }
    /// <summary>
    /// Retrieving CD-DVD Drive Path.
    /// </summary>
    /// <returns></returns>
    public static string GetCdRomDrive()
    {

        ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_CDROMDrive");

        foreach (ManagementObject wmi in searcher.Get())
        {
            try
            {
                return wmi.GetPropertyValue("Drive").ToString();

            }

            catch { }

        }

        return "CD ROM Drive Letter: Unknown";

    }
    /// <summary>
    /// Retrieving BIOS Maker.
    /// </summary>
    /// <returns></returns>
    public static string GetBIOSmaker()
    {

        ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");

        foreach (ManagementObject wmi in searcher.Get())
        {
            try
            {
                return wmi.GetPropertyValue("Manufacturer").ToString();

            }

            catch { }

        }

        return "BIOS Maker: Unknown";

    }
    /// <summary>
    /// Retrieving BIOS Serial No.
    /// </summary>
    /// <returns></returns>
    public static string GetBIOSserNo()
    {

        ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");

        foreach (ManagementObject wmi in searcher.Get())
        {
            try
            {
                return wmi.GetPropertyValue("SerialNumber").ToString();

            }

            catch { }

        }

        return "BIOS Serial Number: Unknown";

    }
    /// <summary>
    /// Retrieving BIOS Caption.
    /// </summary>
    /// <returns></returns>
    public static string GetBIOScaption()
    {

        ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");

        foreach (ManagementObject wmi in searcher.Get())
        {
            try
            {
                return wmi.GetPropertyValue("Caption").ToString();

            }
            catch { }
        }
        return "BIOS Caption: Unknown";
    }
    /// <summary>
    /// Retrieving System Account Name.
    /// </summary>
    /// <returns></returns>
    public static string GetAccountName()
    {

        ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_UserAccount");

        foreach (ManagementObject wmi in searcher.Get())
        {
            try
            {

                return wmi.GetPropertyValue("Name").ToString();
            }
            catch { }
        }
        return "User Account Name: Unknown";

    }
    /// <summary>
    /// Retrieving Physical Ram Memory.
    /// </summary>
    /// <returns></returns>
    public static string GetPhysicalMemory()
    {
        ManagementScope oMs = new ManagementScope();
        ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
        ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
        ManagementObjectCollection oCollection = oSearcher.Get();

        long MemSize = 0;
        long mCap = 0;

        // In case more than one Memory sticks are installed
        foreach (ManagementObject obj in oCollection)
        {
            mCap = Convert.ToInt64(obj["Capacity"]);
            MemSize += mCap;
        }
        MemSize = (MemSize / 1024) / 1024;
        return MemSize.ToString() + "MB";
    }
    /// <summary>
    /// Retrieving No of Ram Slot on Motherboard.
    /// </summary>
    /// <returns></returns>
    public static string GetNoRamSlots()
    {

        int MemSlots = 0;
        ManagementScope oMs = new ManagementScope();
        ObjectQuery oQuery2 = new ObjectQuery("SELECT MemoryDevices FROM Win32_PhysicalMemoryArray");
        ManagementObjectSearcher oSearcher2 = new ManagementObjectSearcher(oMs, oQuery2);
        ManagementObjectCollection oCollection2 = oSearcher2.Get();
        foreach (ManagementObject obj in oCollection2)
        {
            MemSlots = Convert.ToInt32(obj["MemoryDevices"]);

        }
        return MemSlots.ToString();
    }
    //Get CPU Temprature.
    /// <summary>
    /// method for retrieving the CPU Manufacturer
    /// using the WMI class
    /// </summary>
    /// <returns>CPU Manufacturer</returns>
    public static string GetCPUManufacturer()
    {
        string cpuMan = String.Empty;
        //create an instance of the Managemnet class with the
        //Win32_Processor class
        ManagementClass mgmt = new ManagementClass("Win32_Processor");
        //create a ManagementObjectCollection to loop through
        ManagementObjectCollection objCol = mgmt.GetInstances();
        //start our loop for all processors found
        foreach (ManagementObject obj in objCol)
        {
            if (cpuMan == String.Empty)
            {
                // only return manufacturer from first CPU
                cpuMan = obj.Properties["Manufacturer"].Value.ToString();
            }
        }
        return cpuMan;
    }
    /// <summary>
    /// method to retrieve the CPU's current
    /// clock speed using the WMI class
    /// </summary>
    /// <returns>Clock speed</returns>
    public static int GetCPUCurrentClockSpeed()
    {
        int cpuClockSpeed = 0;
        //create an instance of the Managemnet class with the
        //Win32_Processor class
        ManagementClass mgmt = new ManagementClass("Win32_Processor");
        //create a ManagementObjectCollection to loop through
        ManagementObjectCollection objCol = mgmt.GetInstances();
        //start our loop for all processors found
        foreach (ManagementObject obj in objCol)
        {
            if (cpuClockSpeed == 0)
            {
                // only return cpuStatus from first CPU
                cpuClockSpeed = Convert.ToInt32(obj.Properties["CurrentClockSpeed"].Value.ToString());
            }
        }
        //return the status
        return cpuClockSpeed;
    }
    /// <summary>
    /// method to retrieve the network adapters
    /// default IP gateway using WMI
    /// </summary>
    /// <returns>adapters default IP gateway</returns>
    public static string GetDefaultIPGateway()
    {
        //create out management class object using the
        //Win32_NetworkAdapterConfiguration class to get the attributes
        //of the network adapter
        ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
        //create our ManagementObjectCollection to get the attributes with
        ManagementObjectCollection objCol = mgmt.GetInstances();
        string gateway = String.Empty;
        //loop through all the objects we find
        foreach (ManagementObject obj in objCol)
        {
            if (gateway == String.Empty)  // only return MAC Address from first card
            {
                //grab the value from the first network adapter we find
                //you can change the string to an array and get all
                //network adapters found as well
                //check to see if the adapter's IPEnabled
                //equals true
                if ((bool)obj["IPEnabled"] == true)
                {
                    gateway = obj["DefaultIPGateway"].ToString();
                }
            }
            //dispose of our object
            obj.Dispose();
        }
        //replace the ":" with an empty space, this could also
        //be removed if you wish
        gateway = gateway.Replace(":", "");
        //return the mac address
        return gateway;
    }
    /// <summary>
    /// Retrieve CPU Speed.
    /// </summary>
    /// <returns></returns>
    public static double? GetCpuSpeedInGHz()
    {
        double? GHz = null;
        using (ManagementClass mc = new ManagementClass("Win32_Processor"))
        {
            foreach (ManagementObject mo in mc.GetInstances())
            {
                GHz = 0.001 * (UInt32)mo.Properties["CurrentClockSpeed"].Value;
                break;
            }
        }
        return GHz;
    }
    /// <summary>
    /// Retrieving Current Language
    /// </summary>
    /// <returns></returns>
    public static string GetCurrentLanguage()
    {

        ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");

        foreach (ManagementObject wmi in searcher.Get())
        {
            try
            {
                return wmi.GetPropertyValue("CurrentLanguage").ToString();

            }

            catch { }

        }

        return "BIOS Maker: Unknown";

    }
    /// <summary>
    /// Retrieving Current Language.
    /// </summary>
    /// <returns></returns>
    public static string GetOSInformation()
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
        foreach (ManagementObject wmi in searcher.Get())
        {
            try
            {
                return ((string)wmi["Caption"]).Trim() + ", " + (string)wmi["Version"] + ", " + (string)wmi["OSArchitecture"];
            }
            catch { }
        }
        return "BIOS Maker: Unknown";
    }
    /// <summary>
    /// Retrieving Processor Information.
    /// </summary>
    /// <returns></returns>
    public static String GetProcessorInformation()
    {
        ManagementClass mc = new ManagementClass("win32_processor");
        ManagementObjectCollection moc = mc.GetInstances();
        String info = String.Empty;
        foreach (ManagementObject mo in moc)
        {
            string name = (string)mo["Name"];
            name = name.Replace("(TM)", "™").Replace("(tm)", "™").Replace("(R)", "®").Replace("(r)", "®").Replace("(C)", "©").Replace("(c)", "©").Replace("    ", " ").Replace("  ", " ");

            info = name + ", " + (string)mo["Caption"] + ", " + (string)mo["SocketDesignation"];
            //mo.Properties["Name"].Value.ToString();
            //break;
        }
        return info;
    }
    /// <summary>
    /// Retrieving Computer Name.
    /// </summary>
    /// <returns></returns>
    public static String GetComputerName()
    {
        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        ManagementObjectCollection moc = mc.GetInstances();
        String info = String.Empty;
        foreach (ManagementObject mo in moc)
        {
            info = (string)mo["Name"];
            //mo.Properties["Name"].Value.ToString();
            //break;
        }
        return info;
    }

}


using System;
using System.Management;
using System.Windows.Forms;

namespace HardwareInformation
{
    public partial class HardwareInfo_CodeScratcher : Form
    {
        public HardwareInfo_CodeScratcher()
        {
            InitializeComponent();
        }

        private void HardwareInfo_CodeScratcher_Load(object sender, EventArgs e)
        {

        }

        private void ProcId_Click(object sender, EventArgs e)
        {
            lblPid.Text = HardwareInfo.GetProcessorId();
        }

        private void HDDSNo_Click(object sender, EventArgs e)
        {
            lblHDD.Text = HardwareInfo.GetHDDSerialNo();
        }

        private void BoardMake_Click(object sender, EventArgs e)
        {
            lblBM.Text = HardwareInfo.GetBoardMaker();
        }

        private void BIOSMkr_Click(object sender, EventArgs e)
        {
            lblBios.Text = HardwareInfo.GetBIOSmaker();
        }

        private void PhysicalMem_Click(object sender, EventArgs e)
        {
            lblPM.Text = HardwareInfo.GetPhysicalMemory();
        }

        private void CPUSpeed_Click(object sender, EventArgs e)
        {
            lblCS.Text = HardwareInfo.GetCpuSpeedInGHz().ToString();
        }

        private void CPUMkr_Click(object sender, EventArgs e)
        {
            lblCM.Text = HardwareInfo.GetCPUManufacturer();
        }

        

        private void GetCPUManufacturer_Click(object sender, EventArgs e)
        {
            label11.Text = HardwareInfo.GetCPUManufacturer();
        }

        
        private void GetProcessorInfo_Click(object sender, EventArgs e)
        {
            label9.Text = HardwareInfo.GetProcessorInformation();
        }

        private void GetOSInfo_Click(object sender, EventArgs e)
        {
            label10.Text = HardwareInfo.GetOSInformation();
        }

        private void GetCurrentLanguage_Click(object sender, EventArgs e)
        {
            label12.Text = HardwareInfo.GetCurrentLanguage();
        }

        private void GetMyComputerName_Click(object sender, EventArgs e)
        {
            label8.Text = HardwareInfo.GetComputerName();
        }
    }
}

MSDN Reference : Computer System Hardware Classes

Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here