How to Quickly List the Contents of a DirectoryID: Q119396
|
Although Visual Basic has both built-in functions (Dir$ and Dir) and a
built-in control (File list box) for listing the files in a directory,
using a standard list box and the Windows API SendMessage function lists
files in a directory much more quickly.
NOTE: Replacing the File list box or Dir command using the technique
described in this article will only improve performance perceptibly if the
File list box or Dir command is used heavily in the application.
This technique uses the LB_DIR message for list boxes to add a list of
filenames to a list box. To do this, call the Windows API SendMessage
function. The SendMessage function requires the following parameters to
list the files:
SendMessage (hWnd%, LB_DIR, wParam%, lParam)
hWnd% is the handle of the list box.
wParam% is an integer that specifies the permissible file attributes.
lParam is a long pointer to a string containing the full path and
file name to add to the list. This can include wildcards.
Value Meaning
------------------------------------------------------------------------
DDL_ARCHIVE File has been archived.
DDL_DIRECTORY File must be a directory name.
DDL_EXCLUSIVE Exclusive flag. If the exclusive flag is set, only
files of the specified type are listed. Otherwise,
files of the specified type are listed in addition to
files that do not match the specified type.
DDL_HIDDEN File must be hidden.
DDL_READONLY File must be read only.
DDL_READWRITE File can be read from or written to.
DDL_SYSTEM File must be a system file.
' Enter the Declare statement on one single line:
Declare Function SendMessage Lib "user" (ByVal hwnd As Integer,
ByVal wMsg As Integer,
ByVal wParam As Integer,
lParam As Any) As Long
Global Const WM_USER = &H400
Global Const LB_DIR = WM_USER + 14
Global Const DDL_ARCHIVE = &H0020
Global Const DDL_DIRECTORY = &H0010
Global Const DDL_EXCLUSIVE = &H8000
Global Const DDL_HIDDEN = &H0002
Global Const DDL_READONLY = &H0001
Global Const DDL_READWRITE = &H0000
Global Const DDL_SYSTEM = &H0004
Sub Form_Load ()
List1.Clear
s$ = "C:\WINDOWS\*.*"
i% = SendMessage(List1.hWnd, LB_DIR, DDL_HIDDEN, ByVal s$)
End Sub
Additional query words: speed faster 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 11, 1999