如何在C#中快速获取Hardware-ID?

时间:2010-02-25 10:06:50

标签: c# .net hardware cpu hardware-id

我需要在我的程序中将许可证绑定到硬件ID。我尝试过使用WMI,但它仍然很慢。

我需要,例如,CPU,HDD和主板信息。

5 个答案:

答案 0 :(得分:67)

有关详细信息,请参阅this link

以下代码将为您提供CPU ID:

名称空间需要System.Management

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

有关硬盘ID和主板ID详情,请参阅this-link

要加快此过程,请确保您不使用SELECT *,而只选择您真正需要的内容。仅在开发期间使用SELECT *,当您尝试找出需要使用的内容时,因为查询将花费更长时间才能完成。

答案 1 :(得分:19)

我来到这里寻找同样的事情,我找到了另一个解决方案。如果你们有兴趣我分享这个课程:

using System;
using System.Management;
using System.Security.Cryptography;
using System.Security;
using System.Collections;
using System.Text;
namespace Security
{
    /// <summary>
    /// Generates a 16 byte Unique Identification code of a computer
    /// Example: 4876-8DB5-EE85-69D3-FE52-8CF7-395D-2EA9
    /// </summary>
    public class FingerPrint  
    {
        private static string fingerPrint = string.Empty;
        public static string Value()
        {
            if (string.IsNullOrEmpty(fingerPrint))
            {
                fingerPrint = GetHash("CPU >> " + cpuId() + "\nBIOS >> " + 
            biosId() + "\nBASE >> " + baseId()
                            //+"\nDISK >> "+ diskId() + "\nVIDEO >> " + 
            videoId() +"\nMAC >> "+ macId()
                                     );
            }
            return fingerPrint;
        }
        private static string GetHash(string s)
        {
            MD5 sec = new MD5CryptoServiceProvider();
            ASCIIEncoding enc = new ASCIIEncoding();
            byte[] bt = enc.GetBytes(s);
            return GetHexString(sec.ComputeHash(bt));
        }
        private static string GetHexString(byte[] bt)
        {
            string s = string.Empty;
            for (int i = 0; i < bt.Length; i++)
            {
                byte b = bt[i];
                int n, n1, n2;
                n = (int)b;
                n1 = n & 15;
                n2 = (n >> 4) & 15;
                if (n2 > 9)
                    s += ((char)(n2 - 10 + (int)'A')).ToString();
                else
                    s += n2.ToString();
                if (n1 > 9)
                    s += ((char)(n1 - 10 + (int)'A')).ToString();
                else
                    s += n1.ToString();
                if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";
            }
            return s;
        }
        #region Original Device ID Getting Code
        //Return a hardware identifier
        private static string identifier
        (string wmiClass, string wmiProperty, string wmiMustBeTrue)
        {
            string result = "";
            System.Management.ManagementClass mc = 
        new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                if (mo[wmiMustBeTrue].ToString() == "True")
                {
                    //Only get the first one
                    if (result == "")
                    {
                        try
                        {
                            result = mo[wmiProperty].ToString();
                            break;
                        }
                        catch
                        {
                        }
                    }
                }
            }
            return result;
        }
        //Return a hardware identifier
        private static string identifier(string wmiClass, string wmiProperty)
        {
            string result = "";
            System.Management.ManagementClass mc = 
        new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                //Only get the first one
                if (result == "")
                {
                    try
                    {
                        result = mo[wmiProperty].ToString();
                        break;
                    }
                    catch
                    {
                    }
                }
            }
            return result;
        }
        private static string cpuId()
        {
            //Uses first CPU identifier available in order of preference
            //Don't get all identifiers, as it is very time consuming
            string retVal = identifier("Win32_Processor", "UniqueId");
            if (retVal == "") //If no UniqueID, use ProcessorID
            {
                retVal = identifier("Win32_Processor", "ProcessorId");
                if (retVal == "") //If no ProcessorId, use Name
                {
                    retVal = identifier("Win32_Processor", "Name");
                    if (retVal == "") //If no Name, use Manufacturer
                    {
                        retVal = identifier("Win32_Processor", "Manufacturer");
                    }
                    //Add clock speed for extra security
                    retVal += identifier("Win32_Processor", "MaxClockSpeed");
                }
            }
            return retVal;
        }
        //BIOS Identifier
        private static string biosId()
        {
            return identifier("Win32_BIOS", "Manufacturer")
            + identifier("Win32_BIOS", "SMBIOSBIOSVersion")
            + identifier("Win32_BIOS", "IdentificationCode")
            + identifier("Win32_BIOS", "SerialNumber")
            + identifier("Win32_BIOS", "ReleaseDate")
            + identifier("Win32_BIOS", "Version");
        }
        //Main physical hard drive ID
        private static string diskId()
        {
            return identifier("Win32_DiskDrive", "Model")
            + identifier("Win32_DiskDrive", "Manufacturer")
            + identifier("Win32_DiskDrive", "Signature")
            + identifier("Win32_DiskDrive", "TotalHeads");
        }
        //Motherboard ID
        private static string baseId()
        {
            return identifier("Win32_BaseBoard", "Model")
            + identifier("Win32_BaseBoard", "Manufacturer")
            + identifier("Win32_BaseBoard", "Name")
            + identifier("Win32_BaseBoard", "SerialNumber");
        }
        //Primary video controller ID
        private static string videoId()
        {
            return identifier("Win32_VideoController", "DriverVersion")
            + identifier("Win32_VideoController", "Name");
        }
        //First enabled network card ID
        private static string macId()
        {
            return identifier("Win32_NetworkAdapterConfiguration", 
                "MACAddress", "IPEnabled");
        }
        #endregion
    }
}

我不会因此而受到任何赞扬,因为我找到了它here 它的工作速度比我预期的要快。没有图形卡,mac和驱动器ID,我在大约2-3秒内获得了唯一的ID。有了上面的那些,我在大约4-5秒内得到它。

答案 2 :(得分:14)

以下方法受到this answer的启发,涉及相关(更一般)的问题。

方法是读取注册表项MachineGuid中的HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography值。该值在OS安装期间生成。

使用这种方法,每台机器的硬件ID的唯一性几乎没有办法。一种方法是编辑注册表值,但这会导致用户机器出现并发症。另一种方法是克隆驱动器映像,该映像将复制MachineGuid值。

但是,没有一种方法可以防止黑客攻击,这对于普通用户来说肯定是足够好的。从好的方面来说,这种方法在性能上快速且易于实现。

public string GetMachineGuid()
{
   string location = @"SOFTWARE\Microsoft\Cryptography";
   string name = "MachineGuid";

   using (RegistryKey localMachineX64View = 
       RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
   {
       using (RegistryKey rk = localMachineX64View.OpenSubKey(location))
       {
           if (rk == null)
               throw new KeyNotFoundException(
                   string.Format("Key Not Found: {0}", location));

           object machineGuid = rk.GetValue(name);
           if (machineGuid == null)
               throw new IndexOutOfRangeException(
                   string.Format("Index Not Found: {0}", name));

           return machineGuid.ToString();
       }
   }
}

答案 3 :(得分:0)

我们使用了ProcessorID中的处理器ID号(Win32_processor)和UUID中的通用唯一标识符(Win32_ComputerSystemProduct)的组合:

ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select ProcessorID From Win32_processor");
mbsList = mos.Get();
string processorId = string.Empty;
foreach (ManagementBaseObject mo in mbsList)
{
    processorId = mo["ProcessorID"] as string;
}

mos = new ManagementObjectSearcher("SELECT UUID FROM Win32_ComputerSystemProduct");
mbsList = mos.Get();
string systemId = string.Empty;
foreach (ManagementBaseObject mo in mbsList)
{
    systemId = mo["UUID"] as string;
}

var compIdStr = $"{processorId}{systemId}";

以前,我们使用了以下组合:处理器ID("Select ProcessorID From Win32_processor")和主板序列号("SELECT SerialNumber FROM Win32_BaseBoard"),但随后发现主板序列号可能没有填写,或者可以用统一的值填充

  • 由O.E.M.填补
  • 没有
  • 默认字符串

因此,值得考虑这种情况。

还请记住,ProcessorID号在不同的计算机上可能是相同的。

答案 4 :(得分:-2)

Here是一个DLL,显示:
*硬盘ID(驱动器IDE电子芯片中写入的唯一硬件序列号)
*分区ID(卷序列号)
* CPU ID(唯一硬件ID)
* CPU供应商
* CPU运行速度
* CPU理论速度
*内存负载(占总内存的百分比(%))
*总物理(总物理内存,以字节为单位)
*可用物理(物理内存以字节为单位)
* Total PageFile(总页数文件,以字节为单位)
*可用的PageFile(页面文件以字节为单位)
*总虚拟(总虚拟内存,以字节为单位)
*可用虚拟(虚拟内存以字节为单位)
* Bios唯一识别号码BiosDate
* Bios唯一识别号码BiosVersion
* Bios唯一识别号码BiosProductID
* Bios唯一识别号码BiosVideo

(从原始网站抓取的文字)
它适用于C#。