3

I am trying to get the CPU serial number but I cannot do it. I can get board and harddisk but not cpu.

here is my code below. what am I doing wrong?

public static void GetClientComputerInfo()
    {
        HDDSerial = "0";
        BoardSerial = "0";
        CPUSerial = "0";

        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_DiskDrive");

            foreach (ManagementObject share in searcher.Get())
            {
                foreach (PropertyData PC in share.Properties)
                {
                    if (PC.Name == "SerialNumber")
                    {
                        HDDSerial = PC.Value.ToString();
                    }

                    if (PC.Name == "SerialNumber")
                    {
                        BoardSerial = PC.Value.ToString();
                    }

                    if (PC.Name == "ProcessorID")
                    {
                        CPUSerial = PC.Value.ToString();
                    }
                }
            }
        }
        catch
        {

        }
    }
5
  • you are trying to get a serial number, but your CPU section of if statement is checking if (PC.Name == "ProcessorID")? Commented Aug 15, 2014 at 11:20
  • @Thewads: PC.Name changes with every foreach iteration. Commented Aug 15, 2014 at 11:23
  • vcskicks.com/hardware_id.php Commented Aug 15, 2014 at 11:32
  • Win32_DiskDrive doesn't give the Processor ID. You can use "Select * FROM WIN32_Processor" to search for processor ID. Commented Aug 15, 2014 at 11:40
  • PC.Value Can return null then you will get an exception. Use var value = (string)PC.Value Commented Jul 20, 2022 at 6:49

1 Answer 1

8

Try this one

 string cpuInfo = string.Empty;
    ManagementClass mc = new ManagementClass("win32_processor");
    ManagementObjectCollection moc = mc.GetInstances();

    foreach (ManagementObject mo in moc)
    {
         cpuInfo = mo.Properties["processorID"].Value.ToString();
         break;
    }

Code Extracted from here

Sign up to request clarification or add additional context in comments.

4 Comments

Does this 'break' in the foreach-loop make any sense?
For me ManagementObjectCollection contain 2 values, but both are same. I honestly don't know why it had 2 values. I thought it depend on number of Cores of CPU. foreach is not needed in this case. but if you want to print all the values I put a console output inside the for loop without break.
Foreach/break will grab the first value if there is more than one, but prevent an index out of range exception if the collection is empty. This might happen if older systems don't supply a value and you're guessing [0] exists.
In Virtual Box VM cpuInfo = mo.Properties["processorID"].Value.ToString();will throw a NullReferenceException. Use cpuInfo = (string)mo.Properties["processorID"].Value; to avoid mistakes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.