Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to get a unique identifier in C# [duplicate]

I've asked a similar question before, but I'm now reducing some of the restrictions about what I need.

I need to find a unique identifier on a computer, efficiently, with C#. It should always stay the same on any particular computer, and can be a composite of many factors - provided it's simple to retrieve.

I've thought about using the MAC address with WMI Network queries, but this is too slow as usually there are 10+ adapters. Might be better with a WHERE IPEnabled=true clause, but I think there's probably something better than this.

Ideas?

(P.S. It doesn't have to be TOTALLY unique. As long as the chance of collision is small, it's perfect.)

like image 268
Chris Watts Avatar asked Apr 25 '12 06:04

Chris Watts


1 Answers

First Get the Processor ID like following: (Add Reference to System.Management)

    ManagementObjectCollection mbsList = null;
    ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_processor");
    mbsList = mbs.Get();
    string id = "";
    foreach (ManagementObject mo in mbsList)
    {
        id = mo["ProcessorID"].ToString();
    }

//Then you can get the motherboard serial number:

    ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
    ManagementObjectCollection moc = mos.Get();
    string motherBoard = "";
    foreach (ManagementObject mo in moc)
    {
        motherBoard = (string)mo["SerialNumber"];
    }

You can concat the above two and get a unique ID

    string myUniqueID = id + motherBoard;
    Console.WriteLine(myUniqueID);

Also check out this link Finding Hardware ID, CPU ID, Motherboard ID, Hard-Disk ID of a computer

like image 61
Habib Avatar answered Sep 27 '22 18:09

Habib