GetDriveType in C#? or find out if my drive is removable?
Asked Answered
N

4

5

I am using Environment.GetLogicalDrives(); to get a list of drives. I remember in c++ i could use GetDriveType to find if the device was CD, removable, flash, etc and i am thinking i want to put a filter in my app to only show CD and removable on default. What is the GetDriveType equivalent in C#? google only showed me hacks to use the c++ call.

Niddering answered 8/3, 2009 at 8:3 Comment(0)
A
8

Yes, the framework includes a DriveType enumeration used by the DriveInfo class. Have a look at the GetDrives() method on MSDN.

Are answered 8/3, 2009 at 8:8 Comment(0)
M
12

You can use the DriveInfo type to retrieve a list of the drives. You need to check the DriveType property (enum)

var drives = DriveInfo.GetDrives();
foreach (var drive in drives)
{
    if (drive.DriveType == DriveType.Removable)
    {
        Console.WriteLine(drive.Name);
    }
}

You can also use LINQ-to-Objects to query the drives:

var drives = from drive in DriveInfo.GetDrives()
             where drive.DriveType == DriveType.Removable
             select drive;

foreach(var drive in drives)
{
    Console.WriteLine(drive.Name);
}

Like the @TheCodeKing mentioned you can also use WMI to query drive information.

For example, you can query for USB sticks in the following manner:

ManagementObjectCollection drives = new ManagementObjectSearcher(
    "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'"
).Get();

Add a reference to the System.Management assembly if you are going to use WMI.

If you want to fill a ComboBox in a Windows Forms application with this data you need to bind the results to the ComboBox control.

For example:

private void Form1_Load(object sender, EventArgs e)
{
    var drives = from drive in DriveInfo.GetDrives()
                 where drive.DriveType == DriveType.Removable
                 select drive;

    comboBox1.DataSource = drives.ToList();
}

To recapitulate:

  1. Add a ComboBox control to the Windows Form (drag & drop it on the form from the Toolbox)
  2. Query the removable drives.
  3. Bind the results to the ComboBox.
Margertmargery answered 30/8, 2011 at 8:8 Comment(3)
Also note that if you want to include CD-Rom drives, they aren't considered 'Removable' in the DriveType enumeration. You might also want to check || drive.DriveType == DriveType.CDRomVacuole
thank you all for your help but could anyone tell me how to add a drop down menu in my c# winform application and then fill it with the drivers labels and lettersOmaomaha
Updated my answer to show how you can bind these results to a ComboBox control in a Windows Forms application.Margertmargery
A
8

Yes, the framework includes a DriveType enumeration used by the DriveInfo class. Have a look at the GetDrives() method on MSDN.

Are answered 8/3, 2009 at 8:8 Comment(0)
D
2

DriveInfo is the class you are looking for.

Duax answered 8/3, 2009 at 8:8 Comment(0)
M
1

You have use WMI for this, check this link for for information and examples.

Midstream answered 30/8, 2011 at 8:9 Comment(1)
+1 for mentioning WMI. Here's an example using WMI to query drive information: geekpedia.com/…Margertmargery

© 2022 - 2024 — McMap. All rights reserved.