How to

enumerate physical media

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

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

var items = new List<Win32_PhysicalMedia>();
foreach (ManagementObject obj in collection)
{
    var item = new Win32_PhysicalMedia();
    item.Caption = (string)obj["Caption"];
    item.Description = (string)obj["Description"];
    item.InstallDate = (DateTime?)obj["InstallDate"];
    item.Name = (string)obj["Name"];
    item.Status = (string)obj["Status"];
    item.CreationClassName = (string)obj["CreationClassName"];
    item.Manufacturer = (string)obj["Manufacturer"];
    item.Model = (string)obj["Model"];
    item.SKU = (string)obj["SKU"];
    item.SerialNumber = (string)obj["SerialNumber"];
    item.Tag = (string)obj["Tag"];
    item.Version = (string)obj["Version"];
    item.PartNumber = (string)obj["PartNumber"];
    item.OtherIdentifyingInfo = (string)obj["OtherIdentifyingInfo"];
    item.PoweredOn = (bool?)obj["PoweredOn"];
    item.Removable = (bool?)obj["Removable"];
    item.Replaceable = (bool?)obj["Replaceable"];
    item.HotSwappable = (bool?)obj["HotSwappable"];
    item.Capacity = (ulong?)obj["Capacity"];
    item.MediaType = (ushort?)obj["MediaType"];
    item.MediaDescription = (string)obj["MediaDescription"];
    item.WriteProtectOn = (bool?)obj["WriteProtectOn"];
    item.CleanerMedia = (bool?)obj["CleanerMedia"];

    items.Add(item);
}

Class

public class Win32_PhysicalMedia
{
    public string Caption;
    public string Description;
    public DateTime? InstallDate;
    public string Name;
    public string Status;
    public string CreationClassName;
    public string Manufacturer;
    public string Model;
    public string SKU;
    public string SerialNumber;
    public string Tag;
    public string Version;
    public string PartNumber;
    public string OtherIdentifyingInfo;
    public bool? PoweredOn;
    public bool? Removable;
    public bool? Replaceable;
    public bool? HotSwappable;
    public ulong? Capacity;
    public ushort? MediaType;
    public string MediaDescription;
    public bool? WriteProtectOn;
    public bool? CleanerMedia;
}
Send us feedback about this snippet »



Related Snippets: