Converting a volume name to a device name

Windows has a couple of different formats for volume names but it is unclear how to convert a Volumename (example: \\?\Volume{GUID}\) to a DeviceName (example: \Device\HarddiskVolume1).

I found at that you can use the QueryDosDevice function but you need to remove the preceeding \\?\ and the trailing \ of the VolumeName:

Leave a Reply

  1. “\Device\HarddiskVolume1”

    That’s not a device name. It’s a DOS name for the volume.

    is the name for the device. And I’m not finding too many examples on how to get a list volumes on a physicaldriveX or check what PHYSICALDRIVE a volume is on.

  2. Here’s the code to get physicaldrive from volumeguid:

    [DllImport(“Kernel32.dll”, SetLastError = true, CharSet = CharSet.Auto)]
    public static extern SafeFileHandle CreateFile(string fileName, [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess, [MarshalAs(UnmanagedType.U4)] FileShare fileShare, IntPtr securityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, int flags, IntPtr template);

    [DllImport(“kernel32.dll”, ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
    static extern bool DeviceIoControl(Microsoft.Win32.SafeHandles.SafeFileHandle hDevice, uint dwIoControlCode,
    IntPtr lpInBuffer, uint nInBufferSize,
    IntPtr lpOutBuffer, uint nOutBufferSize,
    out uint lpBytesReturned, IntPtr lpOverlapped);

    [DllImport(“Kernel32.dll”, SetLastError = true)]
    internal static extern bool CloseHandle(IntPtr hObject);

    internal struct DISK_EXTENT
    internal int DiskNumber;
    internal long StartingOffset;
    internal long ExtentLength;

    public static string GetPhysicalDrive(string volumeid)
    String Result = String.Empty;
    using (SafeFileHandle device = CreateFile(volumeid, FileAccess.Read, FileShare.Write | FileShare.Read | FileShare.Delete, IntPtr.Zero, FileMode.Open, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_SEQUENTIAL_SCAN, IntPtr.Zero))
    if (device.IsInvalid) return “”;
    int size = 0x400;//some big size
    IntPtr buffer = Marshal.AllocHGlobal(size);
    uint bytesReturned = 0;

    DeviceIoControl(device, 0x00560000, IntPtr.Zero, 0, buffer, (uint)size, out bytesReturned, IntPtr.Zero);
    if (bytesReturned > 0)
    int numberOfDiskExtents = (int)Marshal.PtrToStructure(buffer, typeof(int));
    if (numberOfDiskExtents > 1)
    return String.Empty;
    IntPtr extentPtr = new IntPtr(buffer.ToInt32() + Marshal.SizeOf(typeof(long)));
    DISK_EXTENT extent = (DISK_EXTENT)Marshal.PtrToStructure(extentPtr, typeof(DISK_EXTENT));
    Result = String.Format(“\\\\.\\PHYSICALDRIVE{0}”, extent.DiskNumber);
    return Result;