【问题标题】:How to fast get Hardware-ID in C#?如何在 C# 中快速获取硬件 ID?
【发布时间】:2026-02-13 10:50:01
【问题描述】:

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

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

【问题讨论】:

  • 这样的许可方案对于诚实的客户来说更像是一种烦恼,而不是对破解者的威慑。
  • 请在此处查看答案*.com/a/50907399/3057246

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


【解决方案1】:

更多详情请参考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 *,因为这样查询将花费更多更长的时间才能完成。

【讨论】:

  • 请注意,这些 id 不一定是唯一的,甚至不是存在的。 processorID只是标识处理器设计,VolumeSerialnumber可以由用户更改,也有主板厂商没有设置Serialnumber。
  • 生成唯一id的最佳方法是通过所有这些id的组合,即cpu id,主板id和硬盘id来制作一个id。
  • 谢谢你的回复,但是我觉得WMI还是很慢,你有没有其他办法没有WMI
  • 原帖显示安全(许可)系统需要 ID。但是,答案中提供的代码是从 MS Windows 读取信息。修改这些信息是小菜一碟。但是,Hardware ID Extractor DLL 直接从 CPU(硬件级别)获取信息。您无法更改 CPU 的硬件 ID(除非您熔化 CPU 并重建它:))
  • 基准测试:当我的代码使用 Select * From Win32_processor 时,它需要 ~1s 才能运行,当它使用 Select ProcessorId From Win32_processor 时,它需要 ~0.01s!
【解决方案2】:

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

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 秒内就搞定了。

注意:添加对System.Management的引用。

【讨论】:

  • 下面显示的 DLL 在不到 1 微秒的时间内检索到信息。
  • 这段代码很好。 Tnq
  • 1) 很好.. 2) 如果我在 1 或 2 个月后使用此代码,这是否仍会生成相同的 ID?这会排除可移动 USB 硬盘、USB wifi 适配器、虚拟化软件的虚拟网卡吗??
  • 使用此代码一段时间后 - 存在问题:插入 USB 驱动器将更改密钥,因为它会从任何没有空值的项目中提取信息。实际上整个标识符方法写得不好,为每个属性多次调用它必须每次都创建管理类......
  • 超频时MaxClockSpeed会改变吗?
【解决方案3】:

以下方法的灵感来自this answer 到一个相关(更一般的)问题。

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

使用这种方法解决每台机器的硬件 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();
       }
   }
}

【讨论】:

  • 编辑注册表值很快,not necessarily produces problems on the user machine - 你可以编辑它,做你想做的事(在软件中)并恢复值。
  • 当您通过操作系统映像安装批量系统时,它并不是很有用。您必须为每台机器手动设置 GUID。
【解决方案4】:

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

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 号码在不同的计算机上可能相同。

【讨论】:

    【解决方案5】:

    我重构了 Alex Sutu 方法,使其代码更快更简单:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Management;
    using System.Security.Cryptography;
    
    namespace Test
    {
        /// <summary>
        /// Generates a Guid based on the current computer hardware
        /// Example: C384B159-8E36-6C85-8ED8-6897486500FF
        /// </summary>
        public class SystemGuid
        {
            private static string _systemGuid = string.Empty;
            public static string Value()
            {
                if (string.IsNullOrEmpty(_systemGuid))
                {
                    var lCpuId = GetCpuId();
                    var lBiodId = GetBiosId();
                    var lMainboard = GetMainboardId();
                    var lGpuId = GetGpuId();
                    var lMac = GetMac();
                    var lConcatStr = $"CPU: {lCpuId}\nBIOS:{lBiodId}\nMainboard: {lMainboard}\nGPU: {lGpuId}\nMAC: {lMac}";
                    _systemGuid = GetHash(lConcatStr);
                }
                return _systemGuid;
            }
            private static string GetHash(string s)
            {
                try
                {
                    var lProvider = new MD5CryptoServiceProvider();
                    var lUtf8 = lProvider.ComputeHash(ASCIIEncoding.UTF8.GetBytes(s));
                    return new Guid(lUtf8).ToString().ToUpper();
                }
                catch (Exception lEx)
                {
                    return lEx.Message;
                }
            }
    
            #region Original Device ID Getting Code
        
            //Return a hardware identifier
            private static string GetIdentifier(string pWmiClass, List<string> pProperties)
            {
                string lResult = string.Empty;
                try
                {
                    foreach (ManagementObject lItem in new ManagementClass(pWmiClass).GetInstances())
                    {
                        foreach (var lProperty in pProperties)
                        {
                            try
                            {
                                switch(lProperty)
                                {
                                    case "MACAddress":
                                        if (string.IsNullOrWhiteSpace(lResult) == false)
                                            return lResult; //Return just the first MAC
    
                                        if (lItem["IPEnabled"].ToString() != "True")
                                            continue;
                                    break;
                                }                                
    
                                var lItemProperty = lItem[lProperty];
                                if (lItemProperty == null)
                                    continue;
    
                                var lValue = lItemProperty.ToString();
                                if (string.IsNullOrWhiteSpace(lValue) == false)
                                    lResult += $"{lValue}; ";
                            }
                            catch { }
                        }
    
                    }
                }
                catch{}
                return lResult.TrimEnd(' ', ';');
            }
    
            private static List<string> ListOfCpuProperties = new List<string>{ "UniqueId", "ProcessorId", "Name", "Manufacturer" };
    
            private static string GetCpuId()
            {
                return GetIdentifier("Win32_Processor", ListOfCpuProperties);
            }
    
            private static List<string> ListOfBiosProperties = new List<string> { "Manufacturer", "SMBIOSBIOSVersion", "IdentificationCode", "SerialNumber", "ReleaseDate", "Version" };
            //BIOS Identifier
            private static string GetBiosId()
            {
                return GetIdentifier("Win32_BIOS", ListOfBiosProperties);
            }
    
            private static List<string> ListOfMainboardProperties = new List<string> { "Model", "Manufacturer", "Name", "SerialNumber" };
            //Motherboard ID
            private static string GetMainboardId()
            {
                return GetIdentifier("Win32_BaseBoard", ListOfMainboardProperties);
            }
    
            private static List<string> ListOfGpuProperties = new List<string> { "Name" };
            //Primary video controller ID
            private static string GetGpuId()
            {
                return GetIdentifier("Win32_VideoController", ListOfGpuProperties);
            }
    
            private static List<string> ListOfNetworkProperties = new List<string> { "MACAddress" };
            private static string GetMac()
            {
                return GetIdentifier("Win32_NetworkAdapterConfiguration", ListOfNetworkProperties);
            }
    
            #endregion
        }
    }
    

    【讨论】:

      【解决方案6】:

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

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

      【讨论】:

      • 这个想法是用 C# 来实现的
      • @Frederic - 原始帖子显示安全(许可证)系统需要 ID。但是,答案中提供的代码是从 MS Windows 读取信息。修改这些信息是小菜一碟。但是,Hardware ID Extractor DLL 直接从 CPU(硬件级别)获取信息。您无法更改 CPU 的硬件 ID(除非您熔化 CPU 并重建它:))
      • 此外,它在不到 1 微秒的时间内获取信息。从 MS Windows 检索信息最多需要 5 秒!
      • @FredrikRedin - 无法 C# 导入 DLL?
      • 这是来自 Google 的 VirusTotal.com 的扫描。 62 种防病毒产品中只有 3 种(不那么出名)显示(错误)阳性警报。所有 BIG(卡巴斯基、诺顿、熊猫等)AV 产品都显示文件干净。 virustotal.com/en/file/…