CodeItBetter Programming Another VB Programming Blog

How to get all local drives

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'System & API - How to get all local drives
Option Explicit
 
Public Const DRIVE_ANY = 0
Public Const DRIVE_REMOVABLE = 2
Public Const DRIVE_FIXED = 3
Public Const DRIVE_REMOTE = 4
Public Const DRIVE_CDROM = 5
Public Const DRIVE_RAMDISK = 6
Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
    (ByVal nDrive As String) As Long
 
Public Function GetLocalDrives(lngType As Long) As String()
    On Local Error Resume Next
    Err.Clear
 
    Dim cResult As Long, i As Integer, intCount As Integer
    Dim strTmpArray() As String
    ReDim strTmpArray(0 To 25)
 
    If lngType = DRIVE_ANY Then      'Loop and check for any drive
        For i = 0 To 25
            cResult = GetDriveType( chr ( 65 + i) & ":\")
            If cResult <> 1 Then
                strTmpArray(intCount) =  chr ( 65 + i)
                intCount = intCount + 1
            End If
        Next i
    Else
        'Loop and check for a specific type of drive
        For i = 0 To 25
            cResult = GetDriveType( chr ( 65 + i) & ":\")
            If cResult = lngType Then
                strTmpArray(intCount) =  chr ( 65 + i)
                intCount = intCount + 1
            End If
        Next i
    End If
    'Only redim if one or more drives were found
    If intCount > 0 Then
        ReDim Preserve strTmpArray(0 To intCount - 1)
        GetLocalDrives = strTmpArray
    End If
    For i = LBound(strTmpArray) To UBound(strTmpArray)
        Debug.Print strTmpArray(i)
    Next i
End Function
'How can I call this function:
'Call GetLocalDrives(DRIVE_FIXED)
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.