How to

enumerate disk partitions

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

var items = new List<Win32_DiskPartition>();
foreach (ManagementObject obj in collection)
{
    var item = new Win32_DiskPartition();
    item.Access = (ushort?)obj["Access"];
    item.Availability = (ushort?)obj["Availability"];
    item.BlockSize = (ulong?)obj["BlockSize"];
    item.Bootable = (bool?)obj["Bootable"];
    item.BootPartition = (bool?)obj["BootPartition"];
    item.Caption = (string)obj["Caption"];
    item.ConfigManagerErrorCode = (uint?)obj["ConfigManagerErrorCode"];
    item.ConfigManagerUserConfig = (bool?)obj["ConfigManagerUserConfig"];
    item.CreationClassName = (string)obj["CreationClassName"];
    item.Description = (string)obj["Description"];
    item.DeviceID = (string)obj["DeviceID"];
    item.DiskIndex = (uint?)obj["DiskIndex"];
    item.ErrorCleared = (bool?)obj["ErrorCleared"];
    item.ErrorDescription = (string)obj["ErrorDescription"];
    item.ErrorMethodology = (string)obj["ErrorMethodology"];
    item.HiddenSectors = (uint?)obj["HiddenSectors"];
    item.Index = (uint?)obj["Index"];
    item.InstallDate = (DateTime?)obj["InstallDate"];
    item.LastErrorCode = (uint?)obj["LastErrorCode"];
    item.Name = (string)obj["Name"];
    item.NumberOfBlocks = (ulong?)obj["NumberOfBlocks"];
    item.PNPDeviceID = (string)obj["PNPDeviceID"];
    item.PowerManagementCapabilities = (ushort[])obj["PowerManagementCapabilities"];
    item.PowerManagementSupported = (bool?)obj["PowerManagementSupported"];
    item.PrimaryPartition = (bool?)obj["PrimaryPartition"];
    item.Purpose = (string)obj["Purpose"];
    item.RewritePartition = (bool?)obj["RewritePartition"];
    item.Size = (ulong?)obj["Size"];
    item.StartingOffset = (ulong?)obj["StartingOffset"];
    item.Status = (string)obj["Status"];
    item.StatusInfo = (ushort?)obj["StatusInfo"];
    item.SystemCreationClassName = (string)obj["SystemCreationClassName"];
    item.SystemName = (string)obj["SystemName"];
    item.Type = (string)obj["Type"];

    items.Add(item);
}

Class

public class Win32_DiskPartition
{
    public ushort? Access;
    public ushort? Availability;
    public ulong? BlockSize;
    public bool? Bootable;
    public bool? BootPartition;
    public string Caption;
    public uint? ConfigManagerErrorCode;
    public bool? ConfigManagerUserConfig;
    public string CreationClassName;
    public string Description;
    public string DeviceID;
    public uint? DiskIndex;
    public bool? ErrorCleared;
    public string ErrorDescription;
    public string ErrorMethodology;
    public uint? HiddenSectors;
    public uint? Index;
    public DateTime? InstallDate;
    public uint? LastErrorCode;
    public string Name;
    public ulong? NumberOfBlocks;
    public string PNPDeviceID;
    public ushort[] PowerManagementCapabilities;
    public bool? PowerManagementSupported;
    public bool? PrimaryPartition;
    public string Purpose;
    public bool? RewritePartition;
    public ulong? Size;
    public ulong? StartingOffset;
    public string Status;
    public ushort? StatusInfo;
    public string SystemCreationClassName;
    public string SystemName;
    public string Type;
}
Send us feedback about this snippet »



Related Snippets: