Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# getdrives with type fixed but without usb harddisks?

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?

like image 274
Ephraim Avatar asked Nov 03 '09 08:11

Ephraim


3 Answers

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'");
like image 64
MSalters Avatar answered Sep 30 '22 01:09

MSalters


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?

like image 39
Wael Dalloul Avatar answered Sep 30 '22 00:09

Wael Dalloul


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.

like image 20
Parsa Avatar answered Sep 30 '22 00:09

Parsa