检索每个磁盘的每个分区号和可用空间

时间:2018-06-21 13:04:20

标签: c# windows hard-drive

您好,我使用Win32_DiskDrive获取所有物理磁盘,然后使用Win32_DiskPartition获取物理磁盘分区,

但是我对如何获得每个分区的可用空间以及如何获得每个分区的信感到困惑

试图使用Win32_LogicalDisk,它会检索分区字母和可用空间,

但是没有什么地方可以确定哪个分区字母和大小属于物理驱动器。

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();
    }
}

我在这里检索每个磁盘分区列表。

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();
        }

这是Win32_LogicalDisk输出的内容。

2 个答案:

答案 0 :(得分:0)

这将使用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;
    }

用法:

GetFreeSpace("D:\\");

输出:

274018811904

编辑:

这是一个有输出的方法:

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

代码:

    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;
    }

答案 1 :(得分:0)

好的解决方案在这里:Map a DiskIndex to a Volume Label 技巧是使用DiskPartition类获取DeviceId(即磁盘#0分区#0(示例)),然后使用Win32_LogicalDiskToPartition类使用搜索关键字Antecedent并输出为Dependent,然后使用Win32_LogicalDisk通过搜索关键字DeviceId获取可用空间并完成。 >