List partitions, CD / DVD drives, ... of a computer

Page 1 / 1

For this tutorial, you'll need a "ComboBox" with properties :
- Name : CBOX_drives_list
- DropDownStyle : DropDownList

 

To list the partitions, CD / DVD drives, floppy drives, removable disks, ... you must first import the namespace "System.IO" which contains everything that is used to manage files, ...
To do this, add the following line at the top of the file, before the declaration of your class :

VB.NET

Imports System.IO

Then, you need to use the "IO.DriveInfo.GetDrives()" function which returns an array of drives of type : "IO.DriveInfo". So we'll go with a loop "For Each".

VB.NET

' We through the list of drives
For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives()

  ' Detection drive type
  Dim drive_type As String = ""
  If drive.DriveType = DriveType.Fixed Then
    drive_type = "Local Disk"
  ElseIf drive.DriveType = DriveType.CDRom Then
    drive_type = "CD-Rom drive"
  ElseIf drive.DriveType = DriveType.Network Then
    drive_type = "Network drive"
  ElseIf drive.DriveType = DriveType.Removable Then
    drive_type = "Removable Disk"
  ElseIf drive.DriveType = DriveType.Unknown Then
    drive_type = "Unknown"
  End If
  
  ' The drive name and its type is added to the list of drives
   CBOX_drives_list.Items.Add(drive.Name & " (" & drive_type & ")")
   
Next

' It selects the first item in the list (ComboBox)
CBOX_drives_list.SelectedIndex = 0

Here's a preview of the final result :

Download the source (French Version)