?
Solved

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

Posted on 2006-04-19
6
Medium Priority
?
375 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:JOSHSKORN
  • 4
  • 2
6 Comments
 

Author Comment

by:JOSHSKORN
ID: 16492691
Sorry, the last actual paragraph was a typo.  I did say Combobox from the beginning and accidentally typed in listbox.
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 16496516
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
0
 

Author Comment

by:JOSHSKORN
ID: 16547307
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Accepted Solution

by:
LordWabbit earned 2000 total points
ID: 16548761
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.

0
 

Author Comment

by:JOSHSKORN
ID: 16616586
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.
0
 

Author Comment

by:JOSHSKORN
ID: 16616821
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).
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question