Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi I need to get GUID-HDD and GUID-Processor from my PC, how can I get that with C#?? I'm trying with to get Processor GUID:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;

namespace SerialHDD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ManagementObjectSearcher searcher = new
            ManagementObjectSearcher("SELECT * FROM Win32_Processor");

            foreach (ManagementObject proc in searcher.Get())
            {
                label1.Text = proc["UniqueId"].ToString();
            }
        }
    }
}
This code gives me NulReferenseExeption he can't find value of "UniqueId" I find this parameter from http://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx[^]
Please help me. If sombody knows other way to get GUIDs please write code here.Thanks in advance)).
Posted
Updated 5-Feb-13 6:51am
v2

I have the same result, too. You just don't have to dereference property value (you do it by calling the method ToString in its instance), because the value of the property can be null, legitimately. At least just check for null.

Your approach is not quite correct. Management documentation is good, but it should be confirmed with what runtime gives you. ManagementObject provides some reflective interface: it tells you what properties are available. Use System.Management.ManagementObject.Properties:
http://msdn.microsoft.com/en-us/library/system.management.managementbaseobject.properties.aspx[^].

This way, you can learn what are the available properties and find them all. For example:
C#
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
ManagementObjectCollection items = searcher.Get();
foreach (ManagementObject item in items) {
    foreach (PropertyData property in item.Properties)
        Console.WriteLine("{0}: {1}", property.Name, item[property.Name]);
} //loop


Output it to see what you have. Unique ID is null, yes, but you will see that you can use "ProcessorId", "Name", … You can use other system components for identification, such as HD unique ID…

—SA
 
Share this answer
 
Comments
TD-BRAINIAC 5-Feb-13 14:32pm    
Thanks a lot for answer, I need to know GUID number, as you know all devices has this parameter, but not find function to get it. I can see it from system propoties manualy, but programmatically I don't know how to access to this parametter. If you know other way please answer. Thanks a lot again))!
Sergey Alexandrovich Kryukov 5-Feb-13 14:36pm    
This is what WMI gives you. That's all, I think. Maybe it depends on CPU, I don't know...
—SA
If you're trying to use this to uniquely identify a machine for some security, you're out of luck. It won't work.

First, Intel was the only one to implement the CPUID Serial Number. It doesn't exist on AMD processors. Also, on most Intel-based machines, the CPUID Serial Number is turned OFF by default, so there isn't a value to get!

Also, Intel is not putting the serial number into processors any more. So, it's entirely possible your code is correct, there is no serial number to report!
 
Share this answer
 
Comments
TD-BRAINIAC 5-Feb-13 15:19pm    
You'r right, I'm trying this methods for security to software. Can you list some simple security methods for software?
Dave Kreskowiak 5-Feb-13 17:17pm    
Simple?? No. And I'm not telling you how I'm doing it the hard way in my apps.

You can use a 3rd party component to do this for you. Start be going through these:
http://www.google.com/#hl=en&tbo=d&output=search&sclient=psy-ab&q=licensing+component&oq=licensing+component&gs_l=hp.3..0i30l4.2171.6794.0.7298.19.12.0.7.7.2.222.1509.3j8j1.12.0.les%3B..0.0...1c.1.2.hp.W9qTOaW_6Fo&pbx=1&bav=on.2,or.r_gc.r_pw.r_qf.&bvm=bv.41934586,d.aWM&fp=785d3bc0fb80a0d7&biw=1680&bih=949
Hello,
You will get HDD number using this
C#
ManagementClass mangnmt = new ManagementClass("Win32_LogicalDisk");
            ManagementObjectCollection mcol = mangnmt.GetInstances();
            string result = "";
            foreach (ManagementObject strt in mcol)
            {
                result += Convert.ToString(strt["VolumeSerialNumber"]);
            }
            lblHddSrNo.Text = result;


You will get ProcessorID using this
C#
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;
            }

            lblProcessorId.Text = Id;


Must include
C#
using System.Management;
for these.
May it's helps you...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900