How to get drive information by volume id
Asked Answered
S

4

10

I have a txt file with volume id's in it.

I need to get drive info (drive letter, drive size, etc.) from the drive volume id (Windows):

the volume id is in the following format:

\\?\Volume{XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}

The drive can be Removable/local disk

It doesn't matter how the info is retrieved (it could be script, cpp ,c#, java code).

EDIT:

I tried to use DriveInfo, Win32_LogicalDisk, Win32_Volume, Win32_PnpDevices - but I couldn't find this weird id... in all cases the id has a differrent format

UPDATE:

Found out how to do it.

you can enumerate Win32_Volume like this:

ManagementObjectSearcher ms = new ManagementObjectSearcher("Select * from Win32_Volume");    
foreach(ManagementObject mo in ms.Get())   
{
    var guid = mo["DeviceID"].ToString();

    if(guid == myGuid)
        return mo["DriveLetter"];
}
Shipowner answered 17/4, 2012 at 6:33 Comment(2)
How did u get that id in txt file. Reverse Engineer it to reach the results.Pycno
I am trying to track where "File History" (new feature in windows 8) is savingi its backup files. I found that the target device name is saved in an xml file, it also saves the drive letter and name, but this is not enough because these parameters are not unique and can be changed: somene can eject the device and then in the next time it will have a different letter assigned, etc... so I found that microsoft also saves something that looks like the following: "<TargetVolumePath> \\?\Volume{XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} <\TargetVolumePath>" which is unique id.Shipowner
S
3

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.

Sardinian answered 17/4, 2012 at 7:34 Comment(2)
Can you adda code sample using WMI? I used GetVolumeNameForVolumeMountPoint and it works, but I couldn't make it work using wmi.Shipowner
See the example at msdn.microsoft.com/en-us/library/windows/desktop/…Sardinian
D
2

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

Dearing answered 17/4, 2012 at 6:41 Comment(0)
M
1

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.

Medawar answered 18/10, 2015 at 23:27 Comment(0)
P
-4

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);
    }
}
Peso answered 17/4, 2012 at 6:43 Comment(1)
While this code does list the drives currently installed on the computer, unfortunately it does not answer the question (i.e., how to access drives by GUID), there is no property in DriveInfo that relates to the GUID of a drive.Witcher

© 2022 - 2024 — McMap. All rights reserved.