How to get drive information by volume id

我的未来我决定 提交于 2019-12-05 05:07:45

Volume size, etcetera is easy. Just use the normal Win32 methods. Any function that accepts "C:" as a drive will also accept the volume GUID path (because that's what a \\?\Volume{XXX} is properly called).

The "drive letter" is a bit trickier as there may be 0, 1 or more drive letters. You need to call FindFirstVolumeMountPoint / FindNextVolumeMountPoint / FindVolumeMountPointClose to get all of them.

Try use this

System.Management.ManagementObjectSearcher ms =
new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject mo in ms.Get())
{
    //Find by ID
}

For details reed this Win32_DiskDrive class

There is an API function for this: GetVolumePathNamesForVolumeName

It returns a null terminated array, to allow for multiple mount points. If you have only one mount point (typical), then you can read it as a regular null terminated string.

This is more efficient that enumerating disks/volumes, which could cause idle disks to spin up.

You can use DriveInfo.GetDrives Method to get drive info. Here is the sample code from MSDN

DriveInfo[] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrives)
{
    Console.WriteLine("Drive {0}", d.Name);
    Console.WriteLine("  File type: {0}", d.DriveType);
    if (d.IsReady == true)
    {
        Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
        Console.WriteLine("  File system: {0}", d.DriveFormat);
        Console.WriteLine(
            "  Available space to current user:{0, 15} bytes", 
            d.AvailableFreeSpace);

        Console.WriteLine(
            "  Total available space:          {0, 15} bytes",
            d.TotalFreeSpace);

        Console.WriteLine(
            "  Total size of drive:            {0, 15} bytes ",
            d.TotalSize);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!