Loading Drives w/ types Into Combobox - Error on Code Provided

I'm using Visual Studio .NET 2003 to create a VB Application.

I was attempting to use the following link's accepted answer to assist me with creating a combo box, and loading in a list of only removeable storage drives on a computer (thus omitting Local and Network Drives):

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21286325.html?query=drive+type&topics=866

With the code, I was attempting to modify it such that certain lines weren't as long, by using the statement:

Imports System.Management

In doing that, I received the following message, with System.Management underlined in blue:

Namespace or type 'Management' for Imports 'System.Management' cannot be found.

I know my import statement has been used in other answers on Experts-Exchange, so it's probably safe to assume that it does work.

If I even leave the code as-is, ANY occurance of System.Management in the code produces an error.

Is the code compatible with only a different version of VB .NET?  Am I missing a file?  Can someone produce code for me that will work?  I want my listbox's items to look similar to this:

A:\ - 3 1/2-Inch Floppy Disk
F:\ - CD Drive
JOSHSKORNAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JOSHSKORNAuthor Commented:
Sorry, the last actual paragraph was a typo.  I did say Combobox from the beginning and accidentally typed in listbox.
LordWabbitCommented:
Right click references select Add Reference, wait two days (at least it feels like it on my laptop) scroll down to the System.Management entry and then add it as a reference.  It's not added by default.
And heres some code to get the formatting you want

This is used to get the drives, create the drive object and load it into the list box
            Dim arrDrives() As String = System.IO.Directory.GetLogicalDrives
            Dim intDriveSelected As Integer = 0
            cboDrives.Items.Clear()
            For intIdx As Integer = 0 To arrDrives.GetUpperBound(0)
                Dim oDrive As New HDD
                oDrive.Drive = arrDrives(intIdx)
                If _Path.Substring(0, 1) = arrDrives(intIdx).Substring(0, 1) Then intDriveSelected = intIdx
                cboDrives.Items.Add(oDrive)
            Next

And here is the Class, I've overridded the ToString method so when lobbed into the combo box it all looks nice and pretty.
    Private Class HDD : Inherits Object
        Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Integer
        Private Declare Function GetVolumeInformation Lib "Kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, ByVal lpVolumeSerialNumber As Long, ByVal lpMaximumComponentLength As Long, ByVal lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long
        Public Overrides Function ToString() As String
            Dim strDrive As String = _Drive & " - " & _DriveTypeName
            If _DriveLabel.Trim.Length > 0 Then
                strDrive &= " - " & _DriveLabel
            End If
            Return strDrive
        End Function
        Dim _Drive As String
        Public Property Drive() As String
            Get
                Return _Drive
            End Get
            Set(ByVal Value As String)
                _Drive = Value
                Select Case GetDriveType(_Drive)
                    Case 2
                        _DriveTypeName &= "Removable"
                    Case 3
                        _DriveTypeName &= "Fixed"
                        _DriveLabel = VolumeLabel(_Drive)
                    Case Is = 4
                        _DriveTypeName &= "Remote"
                    Case Is = 5
                        _DriveTypeName &= "Cd-Rom"
                    Case Is = 6
                        _DriveTypeName &= "Ram disk"
                    Case Else
                        _DriveTypeName &= "Unrecognized"
                End Select
            End Set
        End Property
        Dim _DriveTypeName As String
        Public ReadOnly Property DriveTypeName() As String
            Get
                Return _DriveTypeName
            End Get
        End Property
        Dim _DriveLabel As String = ""
        Public ReadOnly Property DriveLabel() As String
            Get
                Return _DriveLabel
            End Get
        End Property
        Private Function VolumeLabel(ByVal Drive As String) As String
            Try
                Dim root As String
                Dim volume_name As String
                Dim serial_number As Integer
                Dim max_component_length As Integer
                Dim file_system_flags As Integer
                Dim file_system_name As String
                Dim pos As Integer
                root = Drive
                volume_name = Space(1024)
                file_system_name = Space(1024)
                If GetVolumeInformation(root, volume_name, _
                    Len(volume_name), serial_number, _
                    max_component_length, file_system_flags, _
                    file_system_name, Len(file_system_name)) = 0 Then
                    Return ""
                End If
                pos = volume_name.IndexOf(Chr(0))
                volume_name = volume_name.Substring(0, pos)
                Return volume_name
            Catch ex As Exception
                '-=- Move on with life
                Return ""
            End Try
        End Function
    End Class
End Class
JOSHSKORNAuthor Commented:
The very first part of the code you've provided, I've placed into my Form Load event.  The line containing '_Path' is producing an error Name '_Path' variable not declared.  I went ahead and added the following statement, which seemed to take care of that problem, and now it's producing another error, but this time, it builds fine, but you see the error upon execution:

New Statement:
Dim _Path As String

New Error (highlighted in Yellow):
An unhandled exception of type 'System.NullReferenceException' occured in myproject.exe.

Additional Informtion: Object reference not set to an instance of an object.

Again, this is the line of code in question:
If _Path.Substring(0, 1) = arrDrives(intIdx).Substring(0, 1) Then intDriveSelected = intIdx

I've even tried moving the line Dim _Path As String to the class level, changing Dim to Private and also Public, still no effect.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

LordWabbitCommented:
The code I have pasted is part of a custom directory browsing control, the _Path is the path you want the control to have selected when you display it, so it takes the first letter of the path (the drive letter) and then while adding the drive entries into the listbox it finds the correct drive to select.  You need to remove two lines for the code to work for eg.

            Dim arrDrives() As String = System.IO.Directory.GetLogicalDrives
            cboDrives.Items.Clear()
            For intIdx As Integer = 0 To arrDrives.GetUpperBound(0)
                Dim oDrive As New HDD
                oDrive.Drive = arrDrives(intIdx)
                 cboDrives.Items.Add(oDrive)
            Next

I see that I didn't comment my code at all!  Must have been in a bit of a rush when I first wrote it.  Sorry about that.
The Inherits from object is a waste of time as well, I was probably initially inheriting from treenode or something and the requirements changed???  Not sure it was a while ago.

Declared reference type variables at class level will not be instantiated, and so will throw a Object reference not set to an instance of an object error when you try to access their members.  It's generally a good idea to assign a value eg. Dim a As String = "", so that if you do end up using them before they are assigned a value they will be instantiated.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JOSHSKORNAuthor Commented:
Ok this partially works.  how do I change this so that only all removable drives appear in the list?  This was my original question.  I've been playing with the values for the variable intIdx, and can only get one Removable drive to appear, the A:\ drive, but others,  such as a USB Flash drive do not appear.  I've placed the code into the Form_Load event, inserted the USB drive into the USB port, verified that the drive registered with the computer and assigned it a drive letter, and then loaded the program.  Still, it does not work (does not show all removable drives).

With the given loop, it shows all drives, and also shows the Floppy A:\ drive and USB drive as removable.
JOSHSKORNAuthor Commented:
Nevermind, I figured it out.  I modified your code to the following:


        Dim arrDrives() As String = System.IO.Directory.GetLogicalDrives

        cboDrives.Items.Clear()
        For intIdx As Integer = 0 To arrDrives.GetUpperBound(0)
            Dim oDrive As New HDD
            oDrive.Drive = arrDrives(intIdx)
            If oDrive.DriveTypeName = "Removable" Then
                cboDrives.Items.Add(oDrive)
            End If
        Next

Thank you LordWabbit.  I'll accept your answer(s).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.