用“获取CPU ID”;ManagementObjectCollection moc=mc.GetInstances()

本文关键字:moc mc GetInstances ManagementObjectCollection 获取 CPU ID | 更新日期: 2023-09-27 17:58:10

我正试图用以下代码获得一个唯一的CPU ID:

private string getCpuId()
    {
        string cpuInfo = string.Empty;
        ManagementClass mc = new ManagementClass("win32_processor");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
            if (cpuInfo == "")
            {
                //Get only the first CPU's ID
                cpuInfo = mo.Properties["processorID"].Value.ToString();
                break;
            }
        }
        return cpuInfo;
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
       MessageBox.Show(getCpuId());
    }

在一年的产品销售过程中,我和600多个其他不同的系统都表现良好。唯一的问题是您必须以管理员身份运行应用程序,这没有问题。2天前,当我在Visual Studio 2013中第一次构建应用程序时,问题就开始了。

在VS 2013之前,我使用的是VS 2012。现在,如果我在VS2013中以管理员身份运行应用程序,它将运行,但在ButtonClick事件中,应用程序停止工作。尝试调试,问题是VS 2013所示的这部分代码:

ManagementObjectCollection moc = mc.GetInstances();

带有以下信息:

OutOfMemoryException was unhandled, an unhandled exception of type 'System.OutOfMemoryException' occurred in System.Management.dll

我搜索了很多,发现了类似的问题和错误,但没有一个像我的。

问题是Visual Studio 2013对我的应用程序做了什么,导致它无法在Visual Studio运行之外工作(我指的是从调试文件夹)?或者是什么真正导致了这个错误,我该如何解决这个问题?我不想转到VS2012,所以任何帮助都将不胜感激。

用“获取CPU ID”;ManagementObjectCollection moc=mc.GetInstances()

问题是Visual Studio的安装,通过重新安装解决了问题。除此之外,我还进行了Windows7更新