I want to only add optical drives to my combo box
but currently my code adds all drives
my code:
Function GetLogicalDrives()
Dim returnValue As String()
returnValue = Directory.GetLogicalDrives
Try
Dim drives As String()
drives = System.IO.Directory.GetLogicalDrives()
Dim str As String
For Each str In drives
ComboBox_cd_drive.Items.Add(str)
Next str
Catch exp As System.IO.IOException
System.Console.WriteLine("An I/O error occurs.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
End Try
End Function
Please help me out and thank you in advance
Loading
AlanPosted Oct 31, 2007, 8:20 AM
The problem there is that the DriveInfo class wasn't introduced until .NET 2.0.
If you're still using .NET 1.1, then you'll have to either use WMI (and the Win32_CDROMDrive class in particular) or call the Win32 API function GetDriveType()directly. The latter is easier and quicker:
Shared Function GetDriveType( ByVal lpRootPathName As String) As Integer
End Function
Const DRIVE_CDROM As Integer = 5
Sub GetLogicalDrives()
Dim returnValue As String()
returnValue = Directory.GetLogicalDrives
Try
Dim drives As String()
drives = System.IO.Directory.GetLogicalDrives()
Dim str As String
Dim driveType As Integer
For Each str In drives
driveType = GetDriveType(str)
If driveType = DRIVE_CDROM Then
ComboBox_cd_drive.Items.Add(str)
End If
Next str
Catch exp As System.IO.IOException
System.Console.WriteLine("An I/O error occurs.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
End Try
End Sub
steve dPosted Oct 30, 2007, 9:33 PM
I am using vb .net 1.1 in visual studio 2003
Type 'System.IO.DriveInfo' is not defined.
Type 'DriveInfo' is not defined.
Name 'DriveType' is not declared.
AlanPosted Oct 30, 2007, 7:32 PM
Try this:
Sub GetLogicalDrives()
Dim returnValue As String()
returnValue = Directory.GetLogicalDrives
Try
Dim drives As String()
drives = System.IO.Directory.GetLogicalDrives()
Dim str As String
Dim di As System.IO.DriveInfo
For Each str In drives
di = New DriveInfo(str)
If di.DriveType = DriveType.CDRom Then
ComboBox_cd_drive.Items.Add(str)
End If
Next str
Catch exp As System.IO.IOException
System.Console.WriteLine("An I/O error occurs.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
End Try
End Sub
Incidentally, I changed the Function to a Sub as you weren't actually returning anything - just listing the drives in the ComboBox.