4

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?

Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
Break Point
  • 309
  • 1
  • 3
  • 5

2 Answers2

7

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

ashes999
  • 9,925
  • 16
  • 73
  • 124
  • 5
    I see no reason to apologize for a late answer here. This isn't a forum, and unanswered question necromancy is encouraged if I'm not mistaken. – Greg Buehler Dec 23 '10 at 15:54
  • I'm trying to make use of this, but I'm slightly flummoxed by the ManagementObjectSearcher only returning the fixed drives even though DriveInfo.GetDrives detects the others. Can you suggest a good resource for understanding how to build the query to detect the rest of them? – Sean Duggan May 12 '14 at 17:02
  • @SeanDuggan I suggest you create a new question to address that. What specifically do you mean by "detects the others" -- what is a non-fixed drive that you expect to see but don't? – ashes999 May 12 '14 at 18:09
  • I have a USB drive and an SD Card inserted, as well as a fixed drive and a network drive. DriveInfo.GetDrives finds all four drives. The code you quote above only gets the fixed drive and the network drive. – Sean Duggan May 12 '14 at 18:50
  • Yep, definitely a new question -- that'll get you better answers, as I'm not exactly a WMI pro. You can try replacing `Win32_DiskDrive` with `Win32_LogicalDisk` or `Win32_MappedLogicalDisk` and see if that data is more useful to you; see [this link](http://msdn.microsoft.com/en-us/library/aa394592(v=vs.85).aspx). – ashes999 May 12 '14 at 19:10
1

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

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Ali Besharati
  • 918
  • 12
  • 25