开发者

Cannot get system identifier in WPF

I programming in WPF C# and trying to get the ProcessorID (or other system identifier). I have read through MSDN - System.Management Namespace. I add the namespace, but it does n开发者_运维问答ot provide ManagementBaseObject Class.

using System.Management;

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

Is System.Management only used in WinForms, and not WPF?


The following code will give you the processor id, given that you have added a reference to 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;  
}


You need to add a reference to System.Management.dll

(Per the "Assembly" in the documentation for that class)


There are some existing types with the System.Management namespace within System.Core, this is why you are seeing some types.

For ManagementBaseObject, however, you will also need to add a reference to System.Management.dll to your project.


The code of Dirk might return a null object. Please correct it in this way:

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜