如果硬盘驱动器被连接直通USB或通过其它方式而不WMI检测而不、硬盘驱动器、方式、WMI

2023-09-05 04:44:46 作者:薄年欲断 YEAR

我想看看连接的驱动器是USB连接,直通SATA,IDE,eSATA或FireWire。有没有办法找到它,而无需使用WMI?我使用这个code,以获得一些信息。

I would like to find out if connected drive is USB connected, thru SATA, IDE, eSATA or Firewire. Is there a way to find it without using WMI? I'm using this code to get some information.

 DriveInfo[] drives = DriveInfo.GetDrives();
 foreach (DriveInfo drive in drives) {
    // DriveType.Fixed && DriveType.Removable

 }

不幸的是这将返回所有硬盘驱动器的连接通USB DriveType.Fixed ,只有U盘怎么着​​也得 DriveType.Removable

Unfortunately this returns for all HDD's connected thru USB DriveType.Fixed, and only USB Sticks get to be DriveType.Removable.

我想我正在寻找这样的事情......只是,我不知道我真正想要的。不幸的是 GetVolumeInformation 不提供认识路的 InterfaceType

I guess I am looking for something like this... just that I don't know what I am actually looking for. Unfortunately GetVolumeInformation doesn't provide the way to know the InterfaceType

    [DllImport("kernel32.dll")]
    private static extern long GetVolumeInformation(
        string PathName, 
        StringBuilder VolumeNameBuffer, 
        UInt32 VolumeNameSize, 
        ref UInt32 VolumeSerialNumber, 
        ref UInt32 MaximumComponentLength, 
        ref UInt32 FileSystemFlags, 
        StringBuilder FileSystemNameBuffer, 
        UInt32 FileSystemNameSize);

我想跳过WMI。我知道,我可以有更多或更少的这code找到它:

I would like to skip WMI. I do know I could find it with more or less this code:

  ManagementObjectSearch theSearcher = new ManagementObjectSearcher(
  "SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");

但我想preFER,以避免它。

but I would prefer to avoid it.

推荐答案

设备管理器API当然能够告诉你哪些总线和控制器的每个磁盘连接到的。但它不是非常的P / Invoke友好。我建议写的code在C ++中这部分,然后做一个C# - 友好界面无论是与C ++ / CLI或设计换的P / Invoke出口。

The Device Manager API is certainly capable of telling you which bus and controller each disk is attached to. But it's not very p/invoke friendly. I suggest writing this portion of the code in C++ and then making a C#-friendly interface either with C++/CLI or designed-for-p/invoke exports.

该解决方案的核心将调用 SetupDiGetDeviceRegistryProperty 来获取与 SPDRP_BUSTYPEGUID 关联的值。但是,获得正确的 HDEVINFO PSP_DEVINFO_DATA 手柄将需要一些工作。

The core of this solution will be calling SetupDiGetDeviceRegistryProperty to fetch the value associated with SPDRP_BUSTYPEGUID. But getting the right HDEVINFO and PSP_DEVINFO_DATA handles will require some work.