N
N
Nikolai Novosad2015-11-03 08:57:08
Visual Basic
Nikolai Novosad, 2015-11-03 08:57:08

Find list of all active drives via GetLogicalDriveStrings?

Hello.
You need to find a list of all active drives via GetLogicalDriveStrings. I found this code on nete:

Public Class Form1
    Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

    Private Function GetDriveStrings() As String
        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
        result = GetLogicalDriveStrings(0, strDrives)
        strDrives = String(result, 0)
        lenStrDrives = result
        result = GetLogicalDriveStrings(lenStrDrives, strDrives)
        If result = 0 Then
            GetDriveStrings = ""
        Else
            GetDriveStrings = strDrives
        End If
    End Function

    Private Sub Command1_Click()
        Dim strDrives As String
        strDrives = GetDriveStrings()
        If strDrives = "" Then
            MsgBox("No Drives were found!", vbCritical)
        Else
            DisplayDriveTypes(strDrives)
        End If
    End Sub

    Private Sub DisplayDriveTypes(drives As String)
        Dim pos As Long
        Dim drive As String
        ListBox1.Clear()
        pos = 1
        Do While Not Mid$(drives, pos, 1) = Chr(0)
            drive = Mid$(drives, pos, 3)
            pos = pos + 4
            ListBox1.AddItem(UCase(drive))
        Loop
    End Sub
End Class

But the following errors appear:
Error 1 "String" is a class type and cannot be used as an expression. C:\Users\vilen\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 9 21 WindowsApplication1
Error 2 Requires "." C:\Users\vilen\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 9 27 WindowsApplication1
Error 3 'Clear' is not a member of 'System.Windows.Forms.ListBox'. C:\Users\vilen\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 32 9 WindowsApplication1
Error 4 'AddItem' is not a member of 'System.Windows.Forms.ListBox'. C:\Users\vilen\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 37 13 WindowsApplication1 I am
using Visual Basic 2010.
How to correct errors?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question