Visual Basic Programming Code Examples
Visual Basic > Files Directories Drives Code Examples
How to fill a listbox with files, directories and drives.
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
50
51
52
53
54
55
56
How to fill a listbox with files, directories and drives.
'Note: This has only been tested with VB 3 & VB 4-16, if you convert
' The SendMessage API provides a simple way to use a
' standard List Box to list files. By sending the LB_DIR
' message to the List Box, the list box automatically
' fills itself with a list of files, directories and drives.
' Create a new project. Add a List box (List1) and a
' Command button (Command1) to the form.
' In a .BAS module add the following declarations:
' Declare API
Declare Function SendMessage& Lib "User" (ByVal hWnd%, ByVal
wMsg%, ByVal wParam%, lParam As Any)
' Note**, The author defined these constants, so you
' won't find them in API documentation, although they
' are based on values used with LB_DIR.
Global Const WM_USER = &H400
Global Const LB_DIR = (WM_USER + 14)
Global Const DIR_NORMALFILES = &H0
Global Const DIR_READONLY = &H8001
Global Const DIR_HIDDEN = &H8002
Global Const DIR_SYSTEM = &H8004
Global Const DIR_DIRECTORIES = &H8010
Global Const DIR_ARCHIVED = &H8020
Global Const DIR_DRIVES = &HC000
' Add this routine to your Form's declaration section:
Sub ListFiles (sFileSpec As String)
Dim i As Long
' Clear existing data
List1.Clear
' Add files / directories of specified types
i = SendMessage(List1.hWnd, LB_DIR, DIR_DRIVES, ByVal sFileSpec)
i = SendMessage(List1.hWnd, LB_DIR, DIR_DIRECTORIES, ByVal sFileSpec)
i = SendMessage(List1.hWnd, LB_DIR, DIR_NORMALFILES, ByVal sFileSpec)
End Sub
' In Command1_Click call the ListFiles routine:
Sub Command1_Click ()
' List all files from C:\Windows directory
Call ListFiles("C:\WINDOWS\*.*")
' Or to list the whole tree for the current drive:
' Call ListFiles("*.*")
End Sub