开发者

How to distinguish between USB and floppy devices?

I'm tryi开发者_运维知识库ng to recognize drives types by looping around DriveInfo.GetDrives() result.

But for both USB and floppy I get the same DriveType.Removable value.

How can I distinguish between them?


You can use WMI (Windows Management Instrumentation) to get more than just what's in the DriveInfo class. In this case, you can get the interface type, which will be "USB" for USB drives.

Sample code is below. You need to add a reference to 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).


The CD Drive And floppy Drive is not ready so you can try this :

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜