Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

How to determine the CD/DVD (optical) drive letter

5.00/5 (4 votes)
28 Jul 2013CPOL 17K  
How to use .NET Framework to determine the drive letter of the CD/DVD (optical) drive.

Introduction

I needed to determine the drive letter of the DVD drive on a PC so that each user did not have to change the .config file to designate the drive letter. I found that the System.IO.DriveInfo.GetDrives() method did not return the optical drive when the drive was not ready. I searched the framework documentation and found another method that returned drive letters.

Background

This example uses the System.Io.Directory.GetLogicalDrives() method to retrieve the configured drive letters. Then, it uses the System.IO.DriveInfo(driveName) method to return a DriveInfo object. The DriveInfo object has a DriveType property.

Using the code

The simple example can be inserted into a VB.NET program or converted to C# using one of the free code converters (e.g., developerFusion Convert VB.NET to C#[^]).

VB
Dim strDVD as String
Try
    Dim Drives As String() = System.IO.Directory.GetLogicalDrives
    For Each strDrive As String In Drives
        Dim di As System.IO.DriveInfo = _
            New System.IO.DriveInfo(strDrive.Substring(0, 1).ToUpper)
        If di.DriveType = System.IO.DriveType.CDRom Then ' Optical Drive (CD or DVD)
            strDVD = strDrive.Substring(0, 2) ' example: F:
        End If
    Next
Catch ex As Exception
    MsgBox("Error while trying to determine the DVD drive letter" & vbNewLine & vbNewLine & _
        ex.Message, MsgBoxStyle.Exclamation, "DVD Drive")
End Try

History

  • Version 1: July 28, 2013.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)