2

我在 WPF C# 中编程并尝试获取 ProcessorID(或其他系统标识符)。我已通读MSDN - System.Management Namespace。我添加了命名空间,但它不提供ManagementBaseObject Class.

using System.Management;

/* code */
System.Management.(there is no ManagementBaseObject)

System.Management 是否仅用于 WinForms,而不是 WPF?

4

4 回答 4

2

您需要添加对 System.Management.dll 的引用

(根据该类文档中的“程序集”)

于 2011-07-28T11:07:07.200 回答
2

System.Management命名空间中存在一些现有类型System.Core,这就是您看到某些类型的原因。

但是,对于ManagementBaseObject,您还需要添加对System.Management.dll项目的引用。

于 2011-07-28T11:08:40.367 回答
2

假设您添加了对 的引用,以下代码将为您提供处理器 ID System.Management

public static string GetProcessorID()
{
    var processorID = "";
    var query = "SELECT ProcessorId FROM Win32_Processor";

    var oManagementObjectSearcher = new ManagementObjectSearcher(query);

    foreach (var oManagementObject in oManagementObjectSearcher.Get())
    {
        processorID = (string)oManagementObject["ProcessorId"];
        break;
    }

    return processorID;  
}
于 2011-07-28T11:13:15.457 回答
0

Dirk 的代码可能会返回一个空对象。请以这种方式更正:

public static string GetProcessorID()
{
    string cpuid = "";
    ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select ProcessorID From Win32_processor");
    foreach (ManagementObject mo in mbs.Get())
    {
        var processorId = mo["ProcessorID"];
        if (processorId != null)
        {
            cpuid = processorId.ToString();
            break;
        }
    }

    return cpuid;
}
于 2020-05-15T14:45:49.153 回答