Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distinguish between USB and floppy devices?

I'm trying 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?

like image 384
Break Point Avatar asked Feb 26 '23 20:02

Break Point


1 Answers

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).

like image 142
ashes999 Avatar answered Mar 07 '23 04:03

ashes999