Get Computer Hardware Information using C#

Today, We want to share with you Get Computer Hardware Information using C#.
In this post we will show you Get Your Hardware Information Using C#, hear for Get PC Hardware Information using C# ASP.NET we will give you demo and example for implement.
In this post, we will learn about How to retrieve basic and advanced hardware and software with an example.

Get Computer Hardware Information using C#

There are the Following The simple About Get Computer Hardware Information using C# Full Information With Example and source code.

As I will cover this Post with live Working example to develop Get Hardware Information using C#, so the some major files and Directory structures for this example is following below.

  • Default.aspx.cs

Get PC Hardware Information

The C# – Computer System Hardware Classes groups Add lots of the classes together that display All the get hardware related some objects. This Good Post some Live Demo how to get/find hardware information in c# in Get Computer Hardware Information 2 different ways.

Get Hardware Information using C#

We May simple use the C# source code To Get Computer Hardware Information System.Management as well as System.Environment namespace for fetching the All lots of the hardware information of a Programming source code machine using C#. Get the below source code simple Copy and Paste for fetching some Hardware Details using C# Programming Language:

How To Get Computer Hardware Information using C# ASP.NET (CPU ID, Hard Disk Serial, MainBoard Info, System Information , …)

This is where I will make a simple HTML form and PHP server side source code for our web application. To make the forms simply all souce code copy and write it into your any text editor Like Notepad++, then save file it as Get Computer Hardware Information – Default.aspx.cs.

Also Read :  PHP Laravel Sessions Example Tutorial

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

//Get Computer Hardware Information methods
public static class GetInfoHardware
{
    public static String GetProcessorId()
    {

        ManagementClass mancl = new ManagementClass("win32_processor");
        ManagementObjectCollection manageobcl = mancl.GetInstances();
        String Id = String.Empty;
        foreach (ManagementObject myObj in manageobcl)
        {

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

    }
    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;
    }
    public static string GetMACAddress()
    {
        ManagementClass mancl = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection manageobcl = mancl.GetInstances();
        string MACAddress = String.Empty;
        foreach (ManagementObject myObj in manageobcl)
        {
            if (MACAddress == String.Empty)
            {
                if ((bool)myObj["IPEnabled"] == true) MACAddress = myObj["MacAddress"].ToString();
            }
            myObj.Dispose();
        }

        MACAddress = MACAddress.Replace(":", "");
        return MACAddress;
    }
    public static string GetBoardMaker()
    {

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

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

            catch { }

        }

        return "Board Maker: Unknown";

    }
    public static string GetBoardProductId()
    {

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

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

            }

            catch { }

        }

        return "Product: Unknown";

    }
    public static string GetCdRomDrive()
    {

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

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

            }

            catch { }

        }

        return "Sorry CD ROM Drive Letter: Unknown";

    }
    public static string GetBIOSmaker()
    {

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

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

            }

            catch { }

        }

        return "BIOS Maker: Unknown";

    }
    public static string GetBIOSserNo()
    {

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

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

            }

            catch { }

        }

        return "BIOS Serial Number: Unknown";

    }
    public static string GetBIOScaption()
    {

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

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

            }
            catch { }
        }
        return "BIOS Caption: Unknown";
    }
    public static string GetAccountName()
    {

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

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

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

    }
    public static string GetPhysicalMemory()
    {
        ManagementScope osmanagesys = new ManagementScope();
        ObjectQuery myobjQry = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
        ManagementObjectSearcher oFinder = new ManagementObjectSearcher(osmanagesys, myobjQry);
        ManagementObjectCollection oCollection = oFinder.Get();

        long liveMSize = 0;
        long livecap = 0;

        foreach (ManagementObject obj in oCollection)
        {
            livecap = Convert.ToInt64(obj["Capacity"]);
            liveMSize += livecap;
        }
        liveMSize = (liveMSize / 1024) / 1024;
        return liveMSize.ToString() + "MB";
    }
    public static string GetNoRamSlots()
    {

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

        }
        return MemSlots.ToString();
    }
	//c# get cpu information
    public static string GetCPUManufacturer()
    {
        string robotCpu = String.Empty;
        ManagementClass managemnt = new ManagementClass("Win32_Processor");
        ManagementObjectCollection objCol = managemnt.GetInstances();
  
        foreach (ManagementObject obj in objCol)
        {
            if (robotCpu == String.Empty)
            {
                robotCpu = obj.Properties["Manufacturer"].Value.ToString();
            }
        }
        return robotCpu;
    }
//Get Computer Hardware Information CPU Clock
    public static int GetCPUCurrentClockSpeed()
    {
        int speedCpuTimestemp = 0;
        ManagementClass managemnt = new ManagementClass("Win32_Processor");
        ManagementObjectCollection objCol = managemnt.GetInstances();
        foreach (ManagementObject obj in objCol)
        {
            if (speedCpuTimestemp == 0)
            {
                speedCpuTimestemp = Convert.ToInt32(obj.Properties["CurrentClockSpeed"].Value.ToString());
            }
        }
        return speedCpuTimestemp;
    }
    public static string GetDefaultIPGateway()
    {

        ManagementClass managemnt = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection objCol = managemnt.GetInstances();
        string gateway = String.Empty;
        foreach (ManagementObject obj in objCol)
        {
            if (gateway == String.Empty)  // only return MAC Address from first card
            {
                if ((bool)obj["IPEnabled"] == true)
                {
                    gateway = obj["DefaultIPGateway"].ToString();
                }
            }
            obj.Dispose();
        }
        gateway = gateway.Replace(":", "");
        return gateway;
    }
    public static double? GetCpuSpeedInGHz()
    {
        double? GHz = null;
        using (ManagementClass mancl = new ManagementClass("Win32_Processor"))
        {
            foreach (ManagementObject myObj in mancl.GetInstances())
            {
                GHz = 0.001 * (UInt32)myObj.Properties["CurrentClockSpeed"].Value;
                break;
            }
        }
        return GHz;
    }
    public static string GetCurrentLanguage()
    {

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

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

            }

            catch { }

        }

        return "BIOS Maker: Unknown";

    }
    public static string GetOSInformation()
    {
        ManagementObjectSearcher finder = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
        foreach (ManagementObject wmi in finder.Get())
        {
            try
            {
                return ((string)wmi["Caption"]).Trim() + ", " + (string)wmi["Version"] + ", " + (string)wmi["OSArchitecture"];
            }
            catch { }
        }
        return "Sorry BIOS Maker: Unknown";
    }
    public static String GetProcessorInformation()
    {
        ManagementClass mancl = new ManagementClass("win32_processor");
        ManagementObjectCollection manageobcl = mancl.GetInstances();
        String info = String.Empty;
        foreach (ManagementObject myObj in manageobcl)
        {
            string name = (string)myObj["Name"];
            name = name.Replace("(TM)", "™").Replace("(tm)", "™").Replace("(R)", "®").Replace("(r)", "®").Replace("(C)", "©").Replace("(c)", "©").Replace("    ", " ").Replace("  ", " ");

            info = name + ", " + (string)myObj["Caption"] + ", " + (string)myObj["SocketDesignation"];

        }
        return info;
    }
    public static String GetComputerName()
    {
        ManagementClass mancl = new ManagementClass("Win32_ComputerSystem");
        ManagementObjectCollection manageobcl = mancl.GetInstances();
        String info = String.Empty;
        foreach (ManagementObject myObj in manageobcl)
        {
            info = (string)myObj["Name"];
            //myObj.Properties["Name"].Value.ToString();
            //break;
        }
        return info;
    }

}

//Fetch Hardware Information Using C#
//Get Computer Hardware Information
using System;
using System.Management;
using System.Windows.Forms;

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

        private void HardwareInfo_CodeScratcher_Load(object sender, EventArgs e)
        {

        }

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

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

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

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

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

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

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

        

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

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

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

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

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

Get Computer Hardware Information using C# – Output

get-hardware-info
get-hardware-info

c# get hardware information – MSDN Reference :

MSDN Reference : C# – Computer System Hardware Classes

Read :

Also Read :  Top 10 Advanced CSS Interview Questions Answers

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How to retrieve basic and advanced hardware and software.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.