1

Hello I'm using Win32_DiskDrive to get all physical disk and then using Win32_DiskPartition to get physical disk partitions,

But I'm stuck with how to do I get free space of each partition and how do I get each partition Letter,

tried to use Win32_LogicalDisk, it retrieves partition letter and free space,

but there is nothing where I could indentify which partition letter and size belongs to physical drive.

ManagementObjectSearcher wmiService = new ManagementObjectSearcher("select Index,MediaType,Model,Partitions,Size,Status,SerialNumber from Win32_DiskDrive where MediaType = 'Removable media' or MediaType = 'External hard disk media' or MediaType = 'Fixed hard disk media'");
foreach (ManagementObject obj in wmiService.Get())
{
    string id = obj["Index"].ToString();
    Console.WriteLine("Index: " + id);
    Console.WriteLine("MediaType: " + obj["MediaType"]);
    Console.WriteLine("Model: " + obj["Model"]);
    Console.WriteLine("Partitions: " + obj["Partitions"]);
    double size = ((Convert.ToDouble(obj["Size"]) / 1024) / 1024) / 1024;//GB
    Console.WriteLine("Size: " + Math.Round(size,1));
    Console.WriteLine("Status: " + obj["Status"]);
    Console.WriteLine("SerialNumber: " + obj["SerialNumber"]);
    Console.WriteLine();

    ManagementObjectSearcher wmiService2 = new ManagementObjectSearcher("select Bootable,BootPartition,DiskIndex,Index,PrimaryPartition,Size from Win32_DiskPartition where DiskIndex = '"+id+"'");
    foreach (ManagementObject obj2 in wmiService2.Get())
    {
        Console.WriteLine("Bootable: " + obj2["Bootable"]);
        Console.WriteLine("BootPartition: " + obj2["BootPartition"]);
        Console.WriteLine("DiskIndex: " + obj2["DiskIndex"]);
        Console.WriteLine("Index: " + obj2["Index"]);
        Console.WriteLine("PrimaryPartition: " + obj2["PrimaryPartition"]);
        double size2 = ((Convert.ToDouble(obj2["Size"]) / 1024) / 1024) / 1024;//GB
        Console.WriteLine("Size: " + Math.Round(size, 1));
        Console.WriteLine();
    }
}

Here I retrieve each disk partition list.

ManagementObjectSearcher wmiService3 = new ManagementObjectSearcher("select * from Win32_LogicalDisk");
        foreach (ManagementObject obj in wmiService3.Get())
        {
            Console.WriteLine("Access: " + obj["Access"]);
            Console.WriteLine("Availability: " + obj["Availability"]);
            Console.WriteLine("BlockSize: " + obj["BlockSize"]);
            Console.WriteLine("Caption: " + obj["Caption"]);
            Console.WriteLine("Compressed: " + obj["Compressed"]);
            Console.WriteLine("ConfigManagerErrorCode: " + obj["ConfigManagerErrorCode"]);
            Console.WriteLine("ConfigManagerUserConfig: " + obj["ConfigManagerUserConfig"]);
            Console.WriteLine("CreationClassName: " + obj["CreationClassName"]);
            Console.WriteLine("Description: " + obj["Description"]);
            Console.WriteLine("DriveType: " + obj["DriveType"]);
            Console.WriteLine("ErrorCleared: " + obj["ErrorCleared"]);
            Console.WriteLine("ErrorDescription: " + obj["ErrorDescription"]);
            Console.WriteLine("ErrorMethodology: " + obj["ErrorMethodology"]);
            Console.WriteLine("FileSystem: " + obj["FileSystem"]);
            Console.WriteLine("FreeSpace: " + obj["FreeSpace"]);
            Console.WriteLine("DeviceID: " + obj["DeviceID"]);
            Console.WriteLine("InstallDate: " + obj["InstallDate"]);
            Console.WriteLine("LastErrorCode: " + obj["LastErrorCode"]);
            Console.WriteLine("MaximumComponentLength: " + obj["MaximumComponentLength"]);
            Console.WriteLine("MediaType: " + obj["MediaType"]);
            Console.WriteLine("Name: " + obj["Name"]);
            Console.WriteLine("NumberOfBlocks: " + obj["NumberOfBlocks"]);
            Console.WriteLine("PNPDeviceID: " + obj["PNPDeviceID"]);
            Console.WriteLine("PowerManagementCapabilities: " + obj["PowerManagementCapabilities"]);
            Console.WriteLine("PowerManagementSupported: " + obj["PowerManagementSupported"]);
            Console.WriteLine("ProviderName: " + obj["ProviderName"]);
            Console.WriteLine("Purpose: " + obj["Purpose"]);
            Console.WriteLine("QuotasDisabled: " + obj["QuotasDisabled"]);
            Console.WriteLine("QuotasIncomplete: " + obj["QuotasIncomplete"]);
            Console.WriteLine("QuotasRebuilding: " + obj["QuotasRebuilding"]);
            Console.WriteLine("Size: " + obj["Size"]);
            Console.WriteLine("Status: " + obj["Status"]);
            Console.WriteLine("StatusInfo: " + obj["StatusInfo"]);
            Console.WriteLine("SupportsDiskQuotas: " + obj["SupportsDiskQuotas"]);
            Console.WriteLine("SupportsFileBasedCompression: " + obj["SupportsFileBasedCompression"]);
            Console.WriteLine("SystemCreationClassName: " + obj["SystemCreationClassName"]);
            Console.WriteLine("SystemName: " + obj["SystemName"]);
            Console.WriteLine("VolumeDirty: " + obj["VolumeDirty"]);
            Console.WriteLine("VolumeName: " + obj["VolumeName"]);
            Console.WriteLine("VolumeSerialNumber: " + obj["VolumeSerialNumber"]);
            Console.WriteLine();
        }

Here is what Win32_LogicalDisk outputs.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
JonZ
  • 139
  • 2
  • 17

2 Answers2

0

This will get the free space of the wanted drive using DriveInfo Class

using System.IO;

private static long GetFreeSpace(string driveName)
    {
        foreach (DriveInfo drive in DriveInfo.GetDrives())
        {
            if (drive.IsReady && drive.Name == driveName)
            {
                return drive.TotalFreeSpace;
            }
        }
        return -1;
    }

usage :

GetFreeSpace("D:\\");

output :

274018811904

EDIT :

Here is a method has an output :

D:\ Drive Total size is 296 GB Free size is 255 GB

Code :

    private static string GetTotalFreeSpace(string driveName)
     {
       foreach (DriveInfo drive in DriveInfo.GetDrives())
       {
           if (drive.IsReady && drive.Name == driveName)
           {
             string TotalSize = (drive.TotalSize / (1024.0 * 1024.0 * 1024.0)).ToString("0");
             string FreeSize = (drive.TotalFreeSpace / (1024.0 * 1024.0 * 1024.0)).ToString("0");
              return string.Format( drive.Name + " Drive Total size is {0} GB Free size is {1} GB",TotalSize,FreeSize);
           }
       }
      return null;
    }
Kaj
  • 806
  • 6
  • 16
  • I used this method problem is I cant set get method for disk partition individually, I need to get this output: Disk #0 Partition #0 is C:/ drive, free space is: 15gb, size:30 gb. – JonZ Jun 21 '18 at 13:28
  • I've updated the answer to suit your needs. You can print whatever you want, or get the partitions in an array and pass it to method. There are many ways to do that. Choose whatever you want. It's up to you – Kaj Jun 21 '18 at 13:46
  • Thats not what I need: like i said output must be, for example we select disk 0 and partition 0 of that disk so we should get partition label,size,and free size, so output should look like this: DISK #0 Partition #0 Label C:/, Partition size: 20GB, Free space: 5GB. @Kaj I apreciate that you try. – JonZ Jun 21 '18 at 14:12
  • If you don't have a time to edit the output like you want, then I don't have also. – Kaj Jun 21 '18 at 14:14
  • out is not what I need DriveInfo output all partitions without even knowing which partition letter belong to physical drive or even partition deviceid from Win32_DiskPartition, good example is windows disk management, there shows EACH DISK and That disk partitions with letters, I'm trying to make same thing. – JonZ Jun 21 '18 at 14:20
  • Like you can't get the result based on the passed Hard drive and etc... – Kaj Jun 21 '18 at 14:24
  • I currently missing only TWO things Letter and partition free space, everything else I got like in my topic, but Win32_LogicalDisk class doesnt have any variable which belongs to Win32_DiskPartition, first idea was to use DriveInfo but its just throws all partitions with letters without even letting me know of which disk it belongs. – JonZ Jun 21 '18 at 14:30
  • Win32_LogicalDisk has the ability to get the partitions info like letter and space, free space, but you have to get them from ManagementScope instance. make the scope to this path : \\\\localhost\\root\\cimv2 and you'll be able to get the details you are missing. – Kaj Jun 21 '18 at 14:57
0

Ok solution is here: Map a DiskIndex to a Volume Label Trick is to use DiskPartition class to get DeviceId which is Disk #0 Partition #0(example), then use Win32_LogicalDiskToPartition class use search key Antecedent and output as Dependent and then use Win32_LogicalDisk to get free space using search key DeviceId and done.

JonZ
  • 139
  • 2
  • 17