1、写一个子程序用于在点击TreeView节点时,在ListView中呈现该节点对应文件夹下的子文件夹和文件。
Private Sub FilterListView(mynode As TreeNode)
....
End Sub
2、先写一段呈现子文件夹的代码:
Private Sub FilterListView(mynode As TreeNode)
ListView1.Items.Clear() '清空ListView
Dim dir As DirectoryInfo = New DirectoryInfo(mynode.Tag.ToString)
Dim item As ListViewItem = Nothing
Dim subitem As ListViewItem.ListViewSubItem() = Nothing
For Each subdir As DirectoryInfo In dir.GetDirectories
item = New ListViewItem(subdir.Name, 0)
Dim subitem As ListViewItem.ListViewSubItem() = Nothing
For Each subdir As DirectoryInfo In dir.GetDirectories
item = New ListViewItem(subdir.Name, 0)
subitem = New ListViewItem.ListViewSubItem() _
{New ListViewItem.ListViewSubItem(item, "文件夹"), _
New ListViewItem.ListViewSubItem(item, subdir.LastWriteTime.ToString)}
item.SubItems.AddRange(subitem)
ListView1.Items.Add(item)
Next
Next
End Sub
3、接着写一段呈现文件的代码:
Private Sub FilterListView(mynode As TreeNode)
ListView1.Items.Clear() '清空ListView
Dim dir As DirectoryInfo = New DirectoryInfo(mynode.Tag.ToString)
Dim item As ListViewItem = Nothing
Dim subitem As ListViewItem.ListViewSubItem() = Nothing
For Each subdir As DirectoryInfo In dir.GetDirectories
item = New ListViewItem(subdir.Name, 0)
Dim subitem As ListViewItem.ListViewSubItem() = Nothing
For Each subdir As DirectoryInfo In dir.GetDirectories
item = New ListViewItem(subdir.Name, 0)
subitem = New ListViewItem.ListViewSubItem() _
{New ListViewItem.ListViewSubItem(item, "文件夹"), _
New ListViewItem.ListViewSubItem(item, subdir.LastWriteTime.ToString)}
item.SubItems.AddRange(subitem)
ListView1.Items.Add(item)
Next
For Each File As FileInfo In dir.GetFiles
item = New ListViewItem(subdir.Name, 0)
subitem = New ListViewItem.ListViewSubItem() _
{New ListViewItem.ListViewSubItem(item, "文件夹"), _
New ListViewItem.ListViewSubItem(item, subdir.LastWriteTime.ToString)}
item.SubItems.AddRange(subitem)
ListView1.Items.Add(item)
Next
Next
End Sub