VB如何获取一个目录下所有文件名?

RT... RT 展开
 我来答
KSPprince
2012-07-12 · TA获得超过1028个赞
知道小有建树答主
回答量:2820
采纳率:33%
帮助的人:376万
展开全部
Module1.bas文件:
Public Const MAX_PATH = 260
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const INVALID_HANDLE_VALUE = -1
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Public Declare Function FindFirstFile Lib "kernel32" Alias
"FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As
WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias
"FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As
WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Function GetFullAllFileFilter(Directory As String) As String
GetFullAllFileFilter = Directory + "\*.*"
End Function
frmMain.frm文件:
VERSION 5.00
Begin VB.Form frmMain
BorderStyle = 3 'Fixed Dialog
Caption = "Enum Files Demo"
ClientHeight = 4770
ClientLeft = 3210
ClientTop = 1905
ClientWidth = 4920
BeginProperty Font
Name = "宋体"
Size = 9
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 4770
ScaleWidth = 4920
ShowInTaskbar = 0 'False
Begin VB.CommandButton cmdList
Caption = "&EnumFiles"
Height = 375
Left = 3600
TabIndex = 3
Top = 1800
Width = 1215
End
Begin VB.DirListBox Dir
Height = 1140
Left = 0
TabIndex = 2
Top = 480
Width = 4935
End
Begin VB.DriveListBox Drive
Height = 300
Left = 0
TabIndex = 1
Top = 120
Width = 4935
End
Begin VB.ListBox lstFileNames
Height = 2400
ItemData = "frmMain.frx":0000
Left = 0
List = "frmMain.frx":0002
TabIndex = 0
Top = 2280
Width = 4935
End
Begin VB.Label Label1
Caption = "Click EnumFiles button to list all files in specified directory."
Height = 495
Left = 120
TabIndex = 4
Top = 1680
Width = 3375
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub cmdList_Click()
Dim lpFileData As WIN32_FIND_DATA ' 保存找到的文件的信息
Dim hFindFile As Long ' 文件查找句柄
Dim FindPattern As String ' 查询的文件模式
Dim tmp As Boolean
' 获得模式
FindPattern = GetFullAllFileFilter(Dir.Path)
' 查找第一个文件
hFindFile = FindFirstFile(FindPattern, lpFileData)
' 如果没有找到
If hFindFile = INVALID_HANDLE_VALUE Then
Exit Sub
End If
' 清除列表
lstFileNames.Clear
' 设置临时变量
tmp = True
Do While tmp
' 如果找到的是文件而不是目录
'(如果连隐藏文件都要一并找出,该条件改为:
' If lpFileData.dwFileAttributes And (FILE_ATTRIBUTE_ARCHIVE OR FILE_ATTRIBUTE_HIDDEN) Then ...)
If lpFileData.dwFileAttributes And FILE_ATTRIBUTE_ARCHIVE Then
' 将文件名添加到列表中
lstFileNames.AddItem lpFileData.cFileName
End If
' 查找下一个文件
tmp = FindNextFile(hFindFile, lpFileData)
Loop
' 全部查询完毕,关闭句柄
FindClose hFindFile
End Sub
Private Sub Drive_Change()
Dir.Path = Drive.Drive
End Sub
Private Sub Form_Load()
Dir.Path = Drive.Drive
End Sub

这样就可以了,你试试
zhongjin175891
2012-07-15 · 超过37用户采纳过TA的回答
知道小有建树答主
回答量:105
采纳率:0%
帮助的人:92.1万
展开全部
用VB的Dir函数也能实现的,这样还要简便些
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
制作问题专家
2012-07-12 · TA获得超过129个赞
知道答主
回答量:109
采纳率:0%
帮助的人:102万
展开全部
楼上何苦那么辛苦呢!只要加一个FileListBox控件修改下Path的值不就好了?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式