5

I want to retrieve the list of fixed disks in a system. But C#s GetDrives Fixed Drives are including plug USB Harddisks.

Any idea how I may detect that a fixed Drive is not an USB harddisk or vica versa?

user229044
  • 232,980
  • 40
  • 330
  • 338
Ephraim
  • 767
  • 1
  • 8
  • 15

4 Answers4

4

Solution nicked from How to get serial number of USB-Stick in C# :

 //import the System.Management namespace at the top in your "using" statement.
 ManagementObjectSearch theSearcher = new ManagementObjectSearcher(
      "SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
Community
  • 1
  • 1
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 1
    Correct, though this will list Sticks and Drives, and a quick look through the properties didn't reveal an easy way to find the Drive letter. – H H Nov 03 '09 at 09:26
  • Apparently ephraim already has those. This solution was intended to show how you'd filter out USB drives. That's why the query was written as `InterfaceType='USB'`, and why it doesn't matter that sticks are included. – MSalters Nov 03 '09 at 09:47
  • There is no way to match the ManagementObjects and the DriveInfo objects because the query does not return drive letters. – Wouter Aug 06 '13 at 11:33
3

use DriveType to detect the type of the drive:

using System.IO;

DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
  if (d.IsReady && d.DriveType == DriveType.Fixed)
  {
    // This is the drive you want...
  }
}

DriveInfo Class

EDIT1:

check the following link: How do I detected whether a hard drive is connected via USB?

Community
  • 1
  • 1
Wael Dalloul
  • 22,172
  • 11
  • 48
  • 57
  • But DriveType.Removable are just USB Sticks not USB Harddisks. From Docu: The drive is a removable storage device, such as a floppy disk drive or a USB flash drive. – Ephraim Nov 03 '09 at 08:54
  • 1
    USB Harddisks are of Type Fixed exactly that is the Problem! – Ephraim Nov 03 '09 at 08:56
  • It's possible. You can check my solution at the bottom. – Parsa Sep 08 '15 at 08:30
1

Here you can get list of USB hard disk.

//Add Reference System.Management and use namespace at the top of the code.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");

        foreach (ManagementObject queryObj in searcher.Get())
        {
            foreach (ManagementObject b in queryObj.GetRelated("Win32_DiskPartition"))
            {
                foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
                { 
                    Console.WriteLine(String.Format("{0}" + "\\", c["Name"].ToString())); // here it will print USB drive letter
                }
            }

        }

Here you can get list of all fixed drives(System and USB hard disks):

        DriveInfo[] allDrives = DriveInfo.GetDrives(); 

        foreach (DriveInfo d in allDrives)
        {
            if (d.IsReady == true && d.DriveType == DriveType.Fixed)
            {
                Console.WriteLine("Drive {0}", d.Name);
                Console.WriteLine("  Drive type: {0}", d.DriveType);   
            }           
        }

If you compare them,then you can retrieve the list of fixed disks in a system but without USB hard disks.

Parsa
  • 1,299
  • 11
  • 17
0

Use this MSDN link for individual solutions (including finding drive letters): WMI Tasks: Disks and File Systems

A.J.Bauer
  • 2,803
  • 1
  • 26
  • 35