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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With