我写了一段乱的代码,只能把文件夹里的文件夹及文件的名称很读取出来,但无法把这些信息反映在Treeview控件中,请高手帮助。
这些代码可以读取文件夹里所有文件夹及文件名称
Public Sub SeachFile(ByVal strPath As String)
On Error Resume Next
Dim i As Integer '文件夹和文件个数
Dim Fso As Object
Dim Fol As Object
Dim Fil As Object
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Fol = Fso.GetFolder(strPath)
Set Fil = Fso.GetFile(strPath)
If strPath <> "" Then
If Right(strPath, 1) = "\" Then
strPath = SLeft(strPath, lstrlen(strPath) - 1)
End If
List2.AddItem SJW(strPath, SMID(strPath))
End If
'扫描文件
For Each Fil In Fol.Files
'一些相关操作
List2.AddItem Fil.Path 'SJW(Fil.Path, SMID(Fil.Path))
Next
'扫描子目錄
For Each Fol In Fol.SubFolders
SeachFile Fol
Next
Set Fso = Nothing
Set Fil = Nothing
End Sub
Private Sub Command2_Click()
SeachFile App.Path & "\文件管理系统"End Sub
Private Function SLeft(S As String, L As Integer) As String '取字符串
Dim i As Integer
For i = 1 To L
SLeft = SLeft & Mid(S, i, 1)
Next
End Function
Private Function SMID(S As String) As Integer '取\的位置
If S = "" Then
SMID = 0
End If
Dim i As Integer
For i = lstrlen(S) To 1 Step -1
If Mid(S, i, 1) = "\" Then
SMID = i
Exit For
End If
Next
If i = 0 Then
SMID = 0
End If
End Function
Private Function SJW(S As String, T As Integer) As String '取\后的字符串
If S = "" Or T = 0 Then
SJW = ""
Exit Function
End If
Dim i As Integer
SJW = Mid(S, T + 1, lstrlen(S) - T)
End Function
这些代码可以读取文件夹里所有文件夹及文件名称
Public Sub SeachFile(ByVal strPath As String)
On Error Resume Next
Dim i As Integer '文件夹和文件个数
Dim Fso As Object
Dim Fol As Object
Dim Fil As Object
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Fol = Fso.GetFolder(strPath)
Set Fil = Fso.GetFile(strPath)
If strPath <> "" Then
If Right(strPath, 1) = "\" Then
strPath = SLeft(strPath, lstrlen(strPath) - 1)
End If
List2.AddItem SJW(strPath, SMID(strPath))
End If
'扫描文件
For Each Fil In Fol.Files
'一些相关操作
List2.AddItem Fil.Path 'SJW(Fil.Path, SMID(Fil.Path))
Next
'扫描子目錄
For Each Fol In Fol.SubFolders
SeachFile Fol
Next
Set Fso = Nothing
Set Fil = Nothing
End Sub
Private Sub Command2_Click()
SeachFile App.Path & "\文件管理系统"End Sub
Private Function SLeft(S As String, L As Integer) As String '取字符串
Dim i As Integer
For i = 1 To L
SLeft = SLeft & Mid(S, i, 1)
Next
End Function
Private Function SMID(S As String) As Integer '取\的位置
If S = "" Then
SMID = 0
End If
Dim i As Integer
For i = lstrlen(S) To 1 Step -1
If Mid(S, i, 1) = "\" Then
SMID = i
Exit For
End If
Next
If i = 0 Then
SMID = 0
End If
End Function
Private Function SJW(S As String, T As Integer) As String '取\后的字符串
If S = "" Or T = 0 Then
SJW = ""
Exit Function
End If
Dim i As Integer
SJW = Mid(S, T + 1, lstrlen(S) - T)
End Function
'模块的代码
'----------------------------
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 Const MAX_PATH = 260
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End TypePublic 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 TypePublic Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
Dim search As WIN32_FIND_DATA
Dim ret As Long
ret = FindFirstFile("C:\*.*", search)
TreeView1.Nodes.Clear
' 设置TREEVIEW的线属性
TreeView1.LineStyle = tvwRootLines ' 增加节点对象
Dim nodX As Node ' 声明一个节点变量
' 第一个节点为C盘,当然可以加入更多的盘,可以用GETDRIVETYPE来检测
Set nodX = TreeView1.Nodes.Add(, , "Root", "C:\")
Dim i As Long
Dim j As Long
j = 0
i = 1
Do While i <> 0
If (search.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
'检测搜索到的文件属性是否是目录
j = j + 1
Set nodX = TreeView1.Nodes.Add("Root", tvwChild, "Child" + Str(j), Trim(search.cFileName))
End If
i = FindNextFile(ret, search)
Loop
nodX.EnsureVisible
loaditem ListView1, "C:"
End SubPrivate Sub TreeView1_Click()
Dim NowNode As Node
Set NowNode = TreeView1.SelectedItem
If NowNode.Text <> "" Then
loaditem ListView1, NowNode.Parent.Text + NowNode.Text
End If
End SubPublic Sub loaditem(List As ListView, path As String)
Dim search As WIN32_FIND_DATA
Dim ret As Long
Dim FindKey As String
FindKey = path + "\*.*"
ret = FindFirstFile(FindKey, search)
Dim Nowitem As ListItem ' 声明LISTVIEW的条目变量.
Dim i As Long
Dim j As Long
j = 0
If ret <> 0 Then
i = 1
Else
i = 0
End If
List.View = lvwList
List.ListItems.Clear
Do While i <> 0
If (search.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) <> FILE_ATTRIBUTE_DIRECTORY Then
j = j + 1
Set Nowitem = List.ListItems.Add(j, , Trim(search.cFileName))
End If
i = FindNextFile(ret, search)
Loop
End Sub