How to

enumerate cdrom drives

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

var items = new List<Win32_CDROMDrive>();
foreach (ManagementObject obj in collection)
{
    var item = new Win32_CDROMDrive();
    item.Availability = (ushort?)obj["Availability"];
    item.Capabilities = (ushort[])obj["Capabilities"];
    item.CapabilityDescriptions = (string[])obj["CapabilityDescriptions"];
    item.Caption = (string)obj["Caption"];
    item.CompressionMethod = (string)obj["CompressionMethod"];
    item.ConfigManagerErrorCode = (uint?)obj["ConfigManagerErrorCode"];
    item.ConfigManagerUserConfig = (bool?)obj["ConfigManagerUserConfig"];
    item.CreationClassName = (string)obj["CreationClassName"];
    item.DefaultBlockSize = (ulong?)obj["DefaultBlockSize"];
    item.Description = (string)obj["Description"];
    item.DeviceID = (string)obj["DeviceID"];
    item.Drive = (string)obj["Drive"];
    item.DriveIntegrity = (bool?)obj["DriveIntegrity"];
    item.ErrorCleared = (bool?)obj["ErrorCleared"];
    item.ErrorDescription = (string)obj["ErrorDescription"];
    item.ErrorMethodology = (string)obj["ErrorMethodology"];
    item.FileSystemFlags = (ushort?)obj["FileSystemFlags"];
    item.FileSystemFlagsEx = (uint?)obj["FileSystemFlagsEx"];
    item.Id = (string)obj["Id"];
    item.InstallDate = (DateTime?)obj["InstallDate"];
    item.LastErrorCode = (uint?)obj["LastErrorCode"];
    item.Manufacturer = (string)obj["Manufacturer"];
    item.MaxBlockSize = (ulong?)obj["MaxBlockSize"];
    item.MaximumComponentLength = (uint?)obj["MaximumComponentLength"];
    item.MaxMediaSize = (ulong?)obj["MaxMediaSize"];
    item.MediaLoaded = (bool?)obj["MediaLoaded"];
    item.MediaType = (string)obj["MediaType"];
    item.MfrAssignedRevisionLevel = (string)obj["MfrAssignedRevisionLevel"];
    item.MinBlockSize = (ulong?)obj["MinBlockSize"];
    item.Name = (string)obj["Name"];
    item.NeedsCleaning = (bool?)obj["NeedsCleaning"];
    item.NumberOfMediaSupported = (uint?)obj["NumberOfMediaSupported"];
    item.PNPDeviceID = (string)obj["PNPDeviceID"];
    item.PowerManagementCapabilities = (ushort[])obj["PowerManagementCapabilities"];
    item.PowerManagementSupported = (bool?)obj["PowerManagementSupported"];
    item.RevisionLevel = (string)obj["RevisionLevel"];
    item.SCSIBus = (uint?)obj["SCSIBus"];
    item.SCSILogicalUnit = (ushort?)obj["SCSILogicalUnit"];
    item.SCSIPort = (ushort?)obj["SCSIPort"];
    item.SCSITargetId = (ushort?)obj["SCSITargetId"];
    item.SerialNumber = (ushort?)obj["SerialNumber"];
    item.Size = (ulong?)obj["Size"];
    item.Status = (string)obj["Status"];
    item.StatusInfo = (ushort?)obj["StatusInfo"];
    item.SystemCreationClassName = (string)obj["SystemCreationClassName"];
    item.SystemName = (string)obj["SystemName"];
    item.TransferRate = (double?)obj["TransferRate"];
    item.VolumeName = (string)obj["VolumeName"];
    item.VolumeSerialNumber = (string)obj["VolumeSerialNumber"];

    items.Add(item);
}

Class

public class Win32_CDROMDrive
{
    public ushort? Availability;
    public ushort[] Capabilities;
    public string[] CapabilityDescriptions;
    public string Caption;
    public string CompressionMethod;
    public uint? ConfigManagerErrorCode;
    public bool? ConfigManagerUserConfig;
    public string CreationClassName;
    public ulong? DefaultBlockSize;
    public string Description;
    public string DeviceID;
    public string Drive;
    public bool? DriveIntegrity;
    public bool? ErrorCleared;
    public string ErrorDescription;
    public string ErrorMethodology;
    public ushort? FileSystemFlags;
    public uint? FileSystemFlagsEx;
    public string Id;
    public DateTime? InstallDate;
    public uint? LastErrorCode;
    public string Manufacturer;
    public ulong? MaxBlockSize;
    public uint? MaximumComponentLength;
    public ulong? MaxMediaSize;
    public bool? MediaLoaded;
    public string MediaType;
    public string MfrAssignedRevisionLevel;
    public ulong? MinBlockSize;
    public string Name;
    public bool? NeedsCleaning;
    public uint? NumberOfMediaSupported;
    public string PNPDeviceID;
    public ushort[] PowerManagementCapabilities;
    public bool? PowerManagementSupported;
    public string RevisionLevel;
    public uint? SCSIBus;
    public ushort? SCSILogicalUnit;
    public ushort? SCSIPort;
    public ushort? SCSITargetId;
    public ushort? SerialNumber;
    public ulong? Size;
    public string Status;
    public ushort? StatusInfo;
    public string SystemCreationClassName;
    public string SystemName;
    public double? TransferRate;
    public string VolumeName;
    public string VolumeSerialNumber;
}
Send us feedback about this snippet »



Related Snippets: