How To Use Visual Basic to List Active Logical Drives (291573)



The information in this article applies to:

  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q291573

SUMMARY

You can determine which drive letters are in use on a local computer by using the GetLogicalDriveStrings Win32 API. This list of drive letters will be the drive letters of all active drives on the local computer, including floppy drives, hard disks, mapped drives, and other drives that are mapped to a drive letter.

MORE INFORMATION

The GetLogicalDriveStrings API function returns a string containing a list of all active drive letters on the local computer. The format of the string is a null-separated list of drive letters with a terminating null at the end of the string. For example, a computer with a single floppy drive (A:) and a single hard disk drive (C:) would have a string of the following format returned by GetLogicalDriveStrings:

A:\<null>C:\<null><null>
					

This string can be parsed to list the active drives.

The following Visual Basic code sample demonstrates how to retrieve and parse a list of drives. Note that this can also be used to create a control or feature similar to the DriveListBox.

Step-by-Step Example

  1. Start a new Visual Basic Standard EXE project. Form1 is created by default.
  2. Add a new command button (Command1) and a listbox (List1) to Form1.
  3. Add the following code to Form1's code window:
    Option Explicit
    
    Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
        Alias "GetLogicalDriveStringsA" _
        (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    
    Private Function GetDriveStrings() As String
        ' Wrapper for calling the GetLogicalDriveStrings API
        
        Dim result As Long          ' Result of our api calls
        Dim strDrives As String     ' String to pass to api call
        Dim lenStrDrives As Long    ' Length of the above string
        
        ' Call GetLogicalDriveStrings with a buffer size of zero to
        ' find out how large our stringbuffer needs to be
        result = GetLogicalDriveStrings(0, strDrives)
        
        strDrives = String(result, 0)
        lenStrDrives = result
        
        ' Call again with our new buffer
        result = GetLogicalDriveStrings(lenStrDrives, strDrives)
        
        If result = 0 Then
            ' There was some error calling the API
            ' Pass back an empty string
            ' NOTE - TODO: Implement proper error handling here
            GetDriveStrings = ""
        Else
            GetDriveStrings = strDrives
        End If
    End Function
    
    Private Sub Command1_Click()
        Dim strDrives As String
        
        ' Find out what drives we have on this machine
        strDrives = GetDriveStrings()
        
        If strDrives = "" Then
            ' No drives were found
            MsgBox "No Drives were found!", vbCritical
        Else
            ' Walk through the string and list each drive
            DisplayDriveTypes strDrives
        End If
    End Sub
    
    Private Sub DisplayDriveTypes(drives As String)
        ' Walk through the logical drive string and display the drive
        ' letters. The logical drive string is a null seperated
        ' double null terminated string.
        
        Dim pos As Long
        Dim drive As String
        
        List1.Clear
        pos = 1
        
        Do While Not Mid$(drives, pos, 1) = Chr(0)
            drive = Mid$(drives, pos, 3)
            pos = pos + 4
            List1.AddItem UCase(drive)
        Loop
    End Sub
  4. Click Run or press the F5 key to run the project.
Result: The drive letter of each logical drive on the system is displayed in the listbox.

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

190000 How To Get Started Programming with the Windows API

291575 How To Use Visual Basic to Locate CD-ROM Drives

196141 PRB: Problems Using Intrinsic File System Controls

The Visual Basic Programmer's Guide (available on MSDN Online, http://msdn.microsoft.com)

Modification Type:MajorLast Reviewed:4/5/2006
Keywords:kbAPI kbhowto kbSample KB291573 kbAudDeveloper