从磁盘号检索虚拟磁盘文件名
当我在 diskpart 中列出虚拟磁盘时:
DISKPART> list vdisk
VDisk ### Disk ### State Type File
--------- -------- -------------------- --------- ----
VDisk 0 Disk 2 Attached not open Fixed C:\Disk.vhd
对我来说有趣的部分是文件名。如果我知道磁盘编号,我试图找到等效的函数,该函数可以为我提供文件名(在文件列下)。
理想情况下,我会给出“\\?\PhysicalDrive2”,结果会得到“C:\Disk.vhd”。
我已经尝试过:
- 使用 diskpart 和解析输出 - 因为它是未记录的格式,所以它可以随时更改。这不是我会依赖的东西。
- 常规 VHD API - 没有函数需要磁盘号作为参数。
- Microsoft.Storage.Vds.dll - 每个驱动器都有枚举(例如 Service.Providers),但没有属性/函数可以提供源文件的名称。虽然我现在可以确定驱动器 D: 是虚拟驱动器,但我仍然不知道附加了哪个 .vhd 文件。
知道可能是什么功能吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是在本地计算机上检索虚拟磁盘并打印其信息的两种解决方案。这两个解决方案演示了如何使用 VDS COM 对象以本机和托管方式访问这些数据。
托管解决方案
我已经从 MSDN 文档 和来自创建了部分 COM 互操作Windows 7 SDK(主要是
vds.h
)。请注意,COM 包装器是部分的,这意味着某些方法尚未移植。下面是一个托管应用程序,它使用 .NET COM 互操作来:
C:\Disk.vhd
)。\\?\PhysicalDrive1
)、友好名称和其他属性。本机解决方案
下面是一个本机应用程序,它使用 VDS COM 接口来:
C:\Disk.vhd
)。\\?\PhysicalDrive1
)、友好名称和其他属性。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.P/调用 GetStorageDependencyInformation 将提供严格的 VHD API 解决方案。虽然此函数不将驱动器号作为输入参数,但包装器方法会。包装器方法将驱动器号转换为“\\\\.\\PhysicalDriveN”形式的字符串,该字符串被传递到
CreateFile
并将生成的句柄传递到 <代码>获取存储依赖信息。类似的包装方法将接受单个char
驱动器号的输入。以下代码从 非托管示例:
P/Invoking the GetStorageDependencyInformation will provide a strictly VHD API solution. While this function does not take a drive number as an input parameter, a wrapper method will. The wrapper method converts a drive number to a string of the form "\\\\.\\PhysicalDriveN" which is passed to
CreateFile
and the resultant handle is passed toGetStorageDependencyInformation
. A similar wrapper method will take an input of a singlechar
drive letter.The following code was translated to C# from an unmanaged example:
虚拟磁盘 API 没有官方托管的 .NET 包装器。因此,您当前有三个选择:
运行 dos 命令并抓取控制台响应,您不想这样做,因为它不是稳定的 API。
使用Server 2008中添加的
Microsoft.Storage.Vds.dll
。您可以使用.NET 反射器来检查 API。然而,这也是非官方的,因为它没有文档记录,因此可能会在服务包等中毫无警告地进行更改。使用官方 C API。在官方托管包装类发布并记录之前,我会建议这样做。如上所述,完整的 API 文档 拥有您需要的一切。我建议围绕这个 API 编写一个简化的 C 包装器 dll,它可以满足您的需要,仅此而已。然后,PInvoke 您的包装器库。
There is no official managed .NET wrapper for the virtual disk APIs. So you currently have three options:
Run the dos command and scrape the console response, which you don't want to do as it is not a stable API.
Use
Microsoft.Storage.Vds.dll
that was added in Server 2008. You can use .NET reflector to examine the API. However, this is also unofficial, in that it is undocumented and thus could change without warning in service packs, etc.Use the official C API. This is what I would recommend until an official managed wrapper class is released and documented. As noted above, the full API documentation has everything you need. I would recommend writing a simplified C wrapper dll around this API that does what you need and no more. Then, PInvoke your wrapper library.
无法在我的计算机上测试虚拟磁盘。不过你可以尝试一下WMI查询。
例如。我可以使用以下代码获取分区信息
它提供了与您的机器相关的几乎所有内容。
您可以尝试使用该工具来获取计算机上可用的命名空间和类:http://www.microsoft.com/downloads/details.aspx?familyid=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=zh
Not able to test vdisk on my machine. But you can give a try to WMI queries.
eg. I can get the partition information using following code
It gives pretty everything related to your machine.
You can try the tool to get namespace and classes available on your machine : http://www.microsoft.com/downloads/details.aspx?familyid=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en