无法在WPF中获取系统标识符

时间:2011-07-28 11:04:10

标签: c# wmi system.management

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

using System.Management;

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

System.Management是仅在WinForms中使用,而不是在WPF中使用吗?

4 个答案:

答案 0 :(得分:2)

您需要添加对System.Management.dll

的引用

(根据该班级的documentation中的“议会”)

答案 1 :(得分:2)

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

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

答案 2 :(得分: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;  
}

答案 3 :(得分: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;
}