简体   繁体   English

如何区分USB和软盘设备?

[英]How to distinguish between USB and floppy devices?

I'm trying to recognize drives types by looping around DriveInfo.GetDrives() result. 我试图通过循环DriveInfo.GetDrives()结果来识别驱动器类型。
But for both USB and floppy I get the same DriveType.Removable value. 但对于USB和软盘,我得到相同的DriveType.Removable值。

How can I distinguish between them? 我该如何区分它们?

You can use WMI (Windows Management Instrumentation) to get more than just what's in the DriveInfo class. 您可以使用WMI(Windows Management Instrumentation)获取的不仅仅是DriveInfo类中的内容。 In this case, you can get the interface type, which will be "USB" for USB drives. 在这种情况下,您可以获得接口类型,它将是USB驱动器的“USB”。

Sample code is below. 示例代码如下。 You need to add a reference to System.Management . 您需要添加对System.Management的引用。

using System.Management;

try
{
    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher("root\\CIMV2",
        "SELECT * FROM Win32_DiskDrive");

    foreach(ManagementObject queryObj in searcher.Get())
    {
        foreach(ManagementObject o in queryObj.GetRelated("Win32_DiskPartition"))
        {
            foreach(ManagementBaseObject b in o.GetRelated("Win32_LogicalDisk"))
            {
                Debug.WriteLine("    #Name: {0}", b["Name"]);
            }
        }
        // One of: USB, IDE
        Debug.WriteLine("Interface: {0}", queryObj["InterfaceType"]);
        Debug.WriteLine("--------------------------------------------");
    }
}
catch (ManagementException f)
{
    Debug.WriteLine(f.StackTrace);
}

For reference, this MSDN page documents the full list of accessible properties (since you don't get autocomplete on this). 作为参考, 此MSDN页面记录了可访问属性的完整列表(因为您没有获得自动完成功能)。

The CD Drive And floppy Drive is not ready so you can try this : CD驱动器和软盘驱动器尚未就绪,因此您可以尝试这样做:

foreach (var dr in DriveInfo.GetDrives())
{
    if (dr.IsReady == true)
    {
        Console.WriteLine(string.Format("name : {0}   type : {1}", dr, dr.DriveType));
    }
}

This is Easy Way to distinguish Between USB and floppy devices 这是区分USB和软盘设备的简便方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM