How to

enumerate memory devices using WMI

Published: 12. February 2012 | Updated: 12. 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

Snippet example shows how to list computer memories.

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_MemoryDevice");
ManagementObjectCollection collection = searcher.Get();

var items = new List<Win32_MemoryDevice>();
foreach (ManagementObject obj in collection)
{
    var item = new Win32_MemoryDevice();
    item.Access = (ushort?)obj["Access"];
    item.AdditionalErrorData = (byte[])obj["AdditionalErrorData"];
    item.Availability = (ushort?)obj["Availability"];
    item.BlockSize = (ulong?)obj["BlockSize"];
    item.Caption = (string)obj["Caption"];
    item.ConfigManagerErrorCode = (uint?)obj["ConfigManagerErrorCode"];
    item.ConfigManagerUserConfig = (bool?)obj["ConfigManagerUserConfig"];
    item.CorrectableError = (bool?)obj["CorrectableError"];
    item.CreationClassName = (string)obj["CreationClassName"];
    item.Description = (string)obj["Description"];
    item.DeviceID = (string)obj["DeviceID"];
    item.EndingAddress = (ulong?)obj["EndingAddress"];
    item.ErrorAccess = (ushort?)obj["ErrorAccess"];
    item.ErrorAddress = (ulong?)obj["ErrorAddress"];
    item.ErrorCleared = (bool?)obj["ErrorCleared"];
    item.ErrorData = (byte[])obj["ErrorData"];
    item.ErrorDataOrder = (ushort?)obj["ErrorDataOrder"];
    item.ErrorDescription = (string)obj["ErrorDescription"];
    item.ErrorGranularity = (ushort?)obj["ErrorGranularity"];
    item.ErrorInfo = (ushort?)obj["ErrorInfo"];
    item.ErrorMethodology = (string)obj["ErrorMethodology"];
    item.ErrorResolution = (ulong?)obj["ErrorResolution"];
    item.ErrorTime = (DateTime?)obj["ErrorTime"];
    item.ErrorTransferSize = (uint?)obj["ErrorTransferSize"];
    item.InstallDate = (DateTime?)obj["InstallDate"];
    item.LastErrorCode = (uint?)obj["LastErrorCode"];
    item.Name = (string)obj["Name"];
    item.NumberOfBlocks = (ulong?)obj["NumberOfBlocks"];
    item.OtherErrorDescription = (string)obj["OtherErrorDescription"];
    item.PNPDeviceID = (string)obj["PNPDeviceID"];
    item.PowerManagementCapabilities = (ushort[])obj["PowerManagementCapabilities"];
    item.PowerManagementSupported = (bool?)obj["PowerManagementSupported"];
    item.Purpose = (string)obj["Purpose"];
    item.StartingAddress = (ulong?)obj["StartingAddress"];
    item.Status = (string)obj["Status"];
    item.StatusInfo = (ushort?)obj["StatusInfo"];
    item.SystemCreationClassName = (string)obj["SystemCreationClassName"];
    item.SystemLevelAddress = (bool?)obj["SystemLevelAddress"];
    item.SystemName = (string)obj["SystemName"];

    items.Add(item);
}

Class

public class Win32_MemoryDevice
{
    public ushort? Access;
    public byte[] AdditionalErrorData;
    public ushort? Availability;
    public ulong? BlockSize;
    public string Caption;
    public uint? ConfigManagerErrorCode;
    public bool? ConfigManagerUserConfig;
    public bool? CorrectableError;
    public string CreationClassName;
    public string Description;
    public string DeviceID;
    public ulong? EndingAddress;
    public ushort? ErrorAccess;
    public ulong? ErrorAddress;
    public bool? ErrorCleared;
    public byte[] ErrorData;
    public ushort? ErrorDataOrder;
    public string ErrorDescription;
    public ushort? ErrorGranularity;
    public ushort? ErrorInfo;
    public string ErrorMethodology;
    public ulong? ErrorResolution;
    public DateTime? ErrorTime;
    public uint? ErrorTransferSize;
    public DateTime? InstallDate;
    public uint? LastErrorCode;
    public string Name;
    public ulong? NumberOfBlocks;
    public string OtherErrorDescription;
    public string PNPDeviceID;
    public ushort[] PowerManagementCapabilities;
    public bool? PowerManagementSupported;
    public string Purpose;
    public ulong? StartingAddress;
    public string Status;
    public ushort? StatusInfo;
    public string SystemCreationClassName;
    public bool? SystemLevelAddress;
    public string SystemName;
}
Send us feedback about this snippet »



Related Snippets: