2

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 not provide ManagementBaseObject Class.

using System.Management;

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

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

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
KMC
  • 19,548
  • 58
  • 164
  • 253

4 Answers4

2

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

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

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
2

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.

Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
2

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;  
}
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • 1
    @KMC: You wouldn't have to, but as it is a method that is independent from any object state it makes sense. Also note, that the `System.Management` namespace is completely independent from whether your application is a console, Windows Forms or WPF application. – Dirk Vollmar Jul 28 '11 at 11:16
0

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;
}
Danilo Carrabino
  • 397
  • 6
  • 12