在没有 WMI 的情况下,如何知道两个分区是否在一个物理硬盘中? [英] Way to know if two partitions are in one physical hard disk without WMI?

查看:26
本文介绍了在没有 WMI 的情况下,如何知道两个分区是否在一个物理硬盘中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有这些分区(在 Windows 中):

I have those partitions (in Windows) for example:

Hard Disk 1 - Partition C, Partition D
Hard Disk 2 - Partition E

在程序语言中有没有办法知道例如分区 C 和分区 D 是否在一个物理硬盘中没有 WMI?

Is there any way in a program language to know if for example partition C and partition D are in one physical hard disk without WMI?

我不想使用 WMI,因为它很慢 - 在这个例子中,我花了 0.5 秒.我需要它快.

I don't want to use WMI because it's slow - for this example, it took for me 0.5 seconds. I need it to be fast.

谢谢.

推荐答案

我不知道有任何其他管理方式来获取磁盘分区信息.您可以使用 C# 中的 P/Invoke 来使用 Win32 API.但是,除非绝对必要,否则您不应该这样做.

I don't know of any other managed way to get disk partition information. You may use the Win32 API using P/Invoke from C#. However, you shouldn't unless it's absolutely necessary.

您需要的 Win32 函数称为 DeviceIoControl().API 文档位于 http://msdn.microsoft.com/en-us/library/aa363216(VS.85).aspx.使用控制代码 IOCTL_STORAGE_GET_DEVICE_NUMBER 调用 DeviceIoControl(),您将获得给定分区设备句柄的物理磁盘驱动器.可以使用 CreateFile() API 检索分区的设备句柄.

The Win32 function you'll need is called DeviceIoControl(). The API documentation can be found at http://msdn.microsoft.com/en-us/library/aa363216(VS.85).aspx. Call DeviceIoControl() with the control code IOCTL_STORAGE_GET_DEVICE_NUMBER and you'll get the physical disk drive for the given partition device handle. The device handle for the partition can be retrieved using CreateFile() API.

但是,使用 DeviceIoControl() 很麻烦,您很可能必须为 32 位和 64 位版本的 Windows 制作不同的版本.

However, using DeviceIoControl() is cumbersome and you will most likely have to make different versions for the 32-bit and 64-bit versions of Windows.

要检索所有分区,您可以像这样使用托管代码 System.IO.DriveInfo:

To retrieve all partitions you may use the managed code System.IO.DriveInfo like this:

var x = from di in DriveInfo.GetDrives()
        where (di.DriveType == DriveType.Fixed)
        select di;

foreach (DriveInfo di in x)
{
    // Call DeviceIoControl() using the partition name from di.Name and the IOCTL_STORAGE_GET_DEVICE_NUMBER  control code to retrieve the physical disk
}

似乎 pinvoke.net 有一些 C# 签名.

It seems pinvoke.net has some signatures for C#.

这篇关于在没有 WMI 的情况下,如何知道两个分区是否在一个物理硬盘中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆