When I list virtual disks within diskpart:
DISKPART> list vdisk
VDisk ### Disk ### State Type File
--------- -------- -------------------- --------- ----
VDisk 0 Disk 2 Attached not open Fixed C:\Disk.vhd
Interesting part for me here is file name. I tried to find equivalent of function that would give me file name (under File column) if I know disk number.
Ideally, I would give “\\?\PhysicalDrive2” and I would get “C:\Disk.vhd” as result.
I already tried:
- Using diskpart and parsing output – since it is undocumented format, it can change at any time. This is not something I would rely on.
- General VHD API – no function takes disk number as parameter.
- Microsoft.Storage.Vds.dll – There are enumerations that go through each drive (e.g. Service.Providers) but there is no property/function that will give me name of source file. While I can now be sure that e.g. drive D: is virtual drive, I still cannot know which .vhd file was attached.
Any idea which function that might be?
Here are two solutions to retrieve virtual disks on the local machine and to print their information. The two solutions demonstrate how to use VDS COM objects to access these data both in a native and managed way.
Managed Solution
I have create a partial COM Interop from the MSDN Documentation and from the Windows 7 SDK (mainly
vds.h). Note that the COM wrappers are partial, which means that some methods are yet to be ported.Below is a managed application that uses the .NET COM interop to:
C:\Disk.vhd).\\?\PhysicalDrive1), its friendly name and other properties.Native Solution
Below is a native application that uses the VDS COM interfaces to:
C:\Disk.vhd).\\?\PhysicalDrive1), its friendly name and other properties.