C# getdrives with type fixed but without usb harddisks?
Asked Answered
B

4

6

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?

Brawn answered 3/11, 2009 at 8:32 Comment(1)
sorry but internet was down, check the below link...Leapt
S
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'");
Savitt answered 3/11, 2009 at 9:4 Comment(3)
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.Hamish
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.Savitt
There is no way to match the ManagementObjects and the DriveInfo objects because the query does not return drive letters.Anastigmatic
L
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?

Leapt answered 3/11, 2009 at 8:40 Comment(3)
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.Brawn
USB Harddisks are of Type Fixed exactly that is the Problem!Brawn
It's possible. You can check my solution at the bottom.Epidemic
E
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.

Epidemic answered 8/9, 2015 at 8:29 Comment(0)
D
0

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

Divergence answered 30/1, 2014 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.