How to

enumerate processors

Published: 8. February 2012 | Updated: 8. February 2012
License: Microsoft Public License (MS-PL)
Categories: Windows » Hardware
Tags: C# Hardware Windows WMI
Was this snippet helpful for you? YESYES / NONO
Get Analyzant!

Check your entire website from code to content in minutes with Analyzant, the free analysis suite.

Free download at analyzant.com

Import namespaces

using System;
using System.Collections.Generic;
using System.Management; // requires adding System.Management reference to project

Code

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
ManagementObjectCollection collection = searcher.Get();

var items = new List<Win32_Processor>();
foreach (ManagementObject obj in collection)
{
    var item = new Win32_Processor();
    item.AddressWidth = (ushort?)obj["AddressWidth"];
    item.Architecture = (ushort?)obj["Architecture"];
    item.Availability = (ushort?)obj["Availability"];
    item.Caption = (string)obj["Caption"];
    item.ConfigManagerErrorCode = (uint?)obj["ConfigManagerErrorCode"];
    item.ConfigManagerUserConfig = (bool?)obj["ConfigManagerUserConfig"];
    item.CpuStatus = (ushort?)obj["CpuStatus"];
    item.CreationClassName = (string)obj["CreationClassName"];
    item.CurrentClockSpeed = (uint?)obj["CurrentClockSpeed"];
    item.CurrentVoltage = (ushort?)obj["CurrentVoltage"];
    item.DataWidth = (ushort?)obj["DataWidth"];
    item.Description = (string)obj["Description"];
    item.DeviceID = (string)obj["DeviceID"];
    item.ErrorCleared = (bool?)obj["ErrorCleared"];
    item.ErrorDescription = (string)obj["ErrorDescription"];
    item.ExtClock = (uint?)obj["ExtClock"];
    item.Family = (ushort?)obj["Family"];
    item.InstallDate = (DateTime?)obj["InstallDate"];
    item.L2CacheSize = (uint?)obj["L2CacheSize"];
    item.L2CacheSpeed = (uint?)obj["L2CacheSpeed"];
    item.L3CacheSize = (uint?)obj["L3CacheSize"];
    item.L3CacheSpeed = (uint?)obj["L3CacheSpeed"];
    item.LastErrorCode = (uint?)obj["LastErrorCode"];
    item.Level = (ushort?)obj["Level"];
    item.LoadPercentage = (ushort?)obj["LoadPercentage"];
    item.Manufacturer = (string)obj["Manufacturer"];
    item.MaxClockSpeed = (uint?)obj["MaxClockSpeed"];
    item.Name = (string)obj["Name"];
    item.NumberOfCores = (uint?)obj["NumberOfCores"];
    item.NumberOfLogicalProcessors = (uint?)obj["NumberOfLogicalProcessors"];
    item.OtherFamilyDescription = (string)obj["OtherFamilyDescription"];
    item.PNPDeviceID = (string)obj["PNPDeviceID"];
    item.PowerManagementCapabilities = (ushort[])obj["PowerManagementCapabilities"];
    item.PowerManagementSupported = (bool?)obj["PowerManagementSupported"];
    item.ProcessorId = (string)obj["ProcessorId"];
    item.ProcessorType = (ushort?)obj["ProcessorType"];
    item.Revision = (ushort?)obj["Revision"];
    item.Role = (string)obj["Role"];
    item.SocketDesignation = (string)obj["SocketDesignation"];
    item.Status = (string)obj["Status"];
    item.StatusInfo = (ushort?)obj["StatusInfo"];
    item.Stepping = (string)obj["Stepping"];
    item.SystemCreationClassName = (string)obj["SystemCreationClassName"];
    item.SystemName = (string)obj["SystemName"];
    item.UniqueId = (string)obj["UniqueId"];
    item.UpgradeMethod = (ushort?)obj["UpgradeMethod"];
    item.Version = (string)obj["Version"];
    item.VoltageCaps = (uint?)obj["VoltageCaps"];

    items.Add(item);
}

Class

public class Win32_Processor
{
    public ushort? AddressWidth;
    public ushort? Architecture;
    public ushort? Availability;
    public string Caption;
    public uint? ConfigManagerErrorCode;
    public bool? ConfigManagerUserConfig;
    public ushort? CpuStatus;
    public string CreationClassName;
    public uint? CurrentClockSpeed;
    public ushort? CurrentVoltage;
    public ushort? DataWidth;
    public string Description;
    public string DeviceID;
    public bool? ErrorCleared;
    public string ErrorDescription;
    public uint? ExtClock;
    public ushort? Family;
    public DateTime? InstallDate;
    public uint? L2CacheSize;
    public uint? L2CacheSpeed;
    public uint? L3CacheSize;
    public uint? L3CacheSpeed;
    public uint? LastErrorCode;
    public ushort? Level;
    public ushort? LoadPercentage;
    public string Manufacturer;
    public uint? MaxClockSpeed;
    public string Name;
    public uint? NumberOfCores;
    public uint? NumberOfLogicalProcessors;
    public string OtherFamilyDescription;
    public string PNPDeviceID;
    public ushort[] PowerManagementCapabilities;
    public bool? PowerManagementSupported;
    public string ProcessorId;
    public ushort? ProcessorType;
    public ushort? Revision;
    public string Role;
    public string SocketDesignation;
    public string Status;
    public ushort? StatusInfo;
    public string Stepping;
    public string SystemCreationClassName;
    public string SystemName;
    public string UniqueId;
    public ushort? UpgradeMethod;
    public string Version;
    public uint? VoltageCaps;
}
Send us feedback about this snippet »



Related Snippets: