C# 我正在尝试获取唯一的 CPU ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15723793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:44:29  来源:igfitidea点击:

I am trying to get an unique CPU ID

c#cpu

提问by user613326

I am using the code below to get a unique CPU ID, i found various samples on the web using this. However. By chance I happen to own 2 Asus Laptops. One is a quad core i5 the other an heavy duty i7 octocore both are 64 bit machines.. To my big surprise they both produce the same "unique" CPU ID ???.

我正在使用下面的代码来获取唯一的 CPU ID,我使用它在网上找到了各种示例。然而。我碰巧拥有 2 台华硕笔记本电脑。一个是四核 i5,另一个是重型 i7 八核,两者都是 64 位机器。令我惊讶的是,它们都产生相同的“唯一”CPU ID ???。

So this code isnt working for me, are there other methods to get unique CPU ids or do i do something wrong here. What I hope to get is a number that specific for each CPU that is made

所以这段代码对我不起作用,是否有其他方法可以获得唯一的 CPU id 或者我在这里做错了什么。我希望得到的是一个特定于每个 CPU 的数字

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

foreach (ManagementObject mo in moc)
{
 if (cpuID == "")
 {
      //Remark gets only the first CPU ID
      cpuID = mo.Properties["processorID"].Value.ToString();

 }
}
return cpuID;

采纳答案by user613326

I came to the conclusion that this is just not a good method to get unique ID's. So instead of this method, I wrote a much larger piece of code in which I requested much more hardware identifiers of the system to build an unique identifier.

我得出的结论是,这不是获得唯一 ID 的好方法。因此,我没有使用这种方法,而是编写了一段更大的代码,其中我请求了更多的系统硬件标识符来构建唯一标识符。

Its way better as this method would even produce different numbers if I would run it on cloned hardware, or if hardware gets changed. There is a lot of WMI info around and I think that's the place where people should look for creating something unique and not use ProcessorID.

它的方式更好,因为如果我在克隆的硬件上运行它,或者如果硬件发生变化,这种方法甚至会产生不同的数字。周围有很多 WMI 信息,我认为这是人们应该寻找创建独特内容而不是使用 ProcessorID 的地方。

回答by DeMama

try

尝试

ManagementClass managClass = new ManagementClass("win32_processor");
ManagementObjectCollection managCollec = managClass.GetInstances();

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

working fine here...

在这里工作正常...

回答by Babak

Although there is a clear machine instruction to ask CPUID of a CPU, this is not guaranteed to return a unique ID, therefore it is virtually impossible to get a universal unique CPUID no matter which method you use. We examined the low level assembly routines and get the same id for two different AMD cpus.

虽然有明确的机器指令询问CPU的CPUID,但这并不能保证返回唯一的ID,因此无论您使用哪种方法,实际上都不可能获得通用的唯一CPUID。我们检查了低级汇编程序,并为两个不同的 AMD cpu 获得了相同的 ID。

回答by deGoot

Maybe the UUIDproperty of the Win32_ComputerSystemProductobject will suit your needs. It is supposed to be unique per-motherboard, as long as the manufacturer configures it in the first place. It's not too common for the CPU to move around without the motherboard coming along for the ride, anyways.

也许对象的UUID属性Win32_ComputerSystemProduct会满足您的需求。只要制造商首先对其进行配置,它就应该是每个主板上唯一的。无论如何,在没有主板陪伴的情况下,CPU 移动并不常见。

回答by Prashant Chavan

    var mbs = new ManagementObjectSearcher("Select ProcessorID From Win32_processor");
    var mbsList = mbs.Get();

    foreach (ManagementObject mo in mbsList)
    {
        cpuid = mo["ProcessorID"].ToString();
        textBox1.Text=cpuid;
    }