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

Posted

技术标签:

【中文标题】检索每个磁盘的每个分区号和可用空间【英文标题】:Retrieving each disk each partition letter and free space 【发布时间】:2018-11-30 19:56:52 【问题描述】:

您好,我使用 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 输出的内容。

【问题讨论】:

如何减少代码以仅显示与您的问题相关的行? 【参考方案1】:

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

【讨论】:

我用这个方法的问题是我不能单独为磁盘分区设置get方法,我需要得到这个输出:Disk #0 Partition #0 is C:/ drive, free space is: 15gb, size :30 GB。 我已经更新了答案以满足您的需求。您可以打印任何您想要的内容,或者获取数组中的分区并将其传递给方法。有很多方法可以做到这一点。选择任何你想要的。这取决于你 这不是我需要的:就像我说的输出必须是,例如我们选择磁盘 0 和该磁盘的分区 0,所以我们应该得到分区标签、大小和空闲大小,所以输出应该看起来像这样:DISK #0 Partition #0 Label C:/,分区大小:20GB,可用空间:5GB。 @Kaj 我很欣赏你的尝试。 如果你没有时间编辑你想要的输出,那么我也没有。 out 不是我需要的 DriveInfo 输出所有分区,甚至不知道哪个分区号属于物理驱动器,甚至不知道 Win32_DiskPartition 的分区 deviceid,很好的例子是 Windows 磁盘管理,显示每个磁盘和磁盘分区用字母,我正在尝试做同样的事情。【参考方案2】:

好的解决方案在这里:Map a DiskIndex to a Volume Label 技巧是使用 DiskPartition 类获取 DeviceId 即 Disk #0 Partition #0(example),然后使用 Win32_LogicalDiskToPartition 类使用搜索键 Antecedent 并输出为 Dependent,然后使用 Win32_LogicalDisk 使用搜索键 DeviceId 获取可用空间并完成。

【讨论】:

以上是关于检索每个磁盘的每个分区号和可用空间的主要内容,如果未能解决你的问题,请参考以下文章

如何在 MacOS (sierra-catalina) 上为分区获取可用空间

分区持久区域的每个成员所需的存储空间

NO.25磁盘分区及格式化扩展Swap空间文件系统管理

linux磁盘空间不足

安装centos7时手动分区,磁盘可用空间不足

linux基础命令---df