2

我正在尝试使用此代码:

public string GetCPUId()
{
    string cpuInfo = String.Empty;
    string temp = String.Empty;
    ManagementClass mc = new ManagementClass("Win32_Processor");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject mo in moc)
    {
        if (cpuInfo == String.Empty)
        {
            cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
        }
    }
    return cpuInfo;
}

在 XP 虚拟机(virtualbox)上获取硬件 uid,但我只得到一个消息框,上面写着:

Object reference not set to an instance of an object.

是因为它是虚拟机还是什么?

4

3 回答 3

7

是的,这是因为您正在运行虚拟机。 mo.Properties["ProcessorId"]将返回空值。在这里查看答案。

于 2011-02-24T19:13:53.570 回答
3

我刚刚在这里找到了一个更快的解决方案:http: //www.dotnetspark.com/kb/24-get-processor-id-using-c-sharp.aspx

it works faster than yours.and IT WORKS IN MY VIRTUAL WINDOWS(using VMware Workstation 7.0.0 with WINDOWS XP installed virtually) as both codes use the same library yours should work as well! try including dll file in project output it MIGHT Help.

于 2011-10-26T11:43:33.973 回答
0

这在虚拟机上应该可以正常工作。但是,虚拟 CPU 提供的 CPU ID 可能与物理 CPU 匹配,也可能不匹配。

于 2011-02-24T19:14:33.097 回答