VB.net TreeView 读取系统文件名

前端之家收集整理的这篇文章主要介绍了VB.net TreeView 读取系统文件名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Imports System.IO
Imports System.Data
'Imports System.Data.Linq
'Imports System.Data.Linq.Mapping
Imports System.Windows.Forms

Public Class Form1

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object,ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If Not TreeView1.SelectedNode Is Nothing Then
MessageBox.Show(TreeView1.SelectedNode.Text)
End If

End Sub

Public Sub AddDirectory(ByVal strFatherPath As String,ByVal strPath As String,ByVal nodeFather As TreeNode)

Dim iDir As IO.Directory

Dim iDirInfo As IO.DirectoryInfo

Dim i As Integer

Dim node As New TreeNode

'先添加本目录,从文件夹路径分析出文件名称

node.Text = Strings.Replace(strPath,strFatherPath & "\","",1)

'为单个节点指定节点未被选中时显示的图标

node.ImageIndex = 1

'为单个节点指定节点被选中时显示的图标

node.SelectedImageIndex = 2

nodeFather.Nodes.Add(node)

Application.DoEvents()

Try

Dim str() As String = IO.Directory.GetDirectories(strPath)

'遍历该目录的子文件

For i = 0 To str.GetUpperBound(0)

AddDirectory(strPath,str(i),node)

Next

Catch ex As Exception

Debug.WriteLine(ex.Message)

End Try

node = Nothing

iDir = Nothing

End Sub

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click

Dim iDir As IO.Directory

Dim node As New TreeNode

'先把C盘添加到树中

TreeView1.Nodes.Clear()

node.ImageIndex = 0

node.Text = "PC_Code"

node.SelectedImageIndex = -1

TreeView1.Nodes.Add(node)

Dim i As Integer

'获取C:盘根目录下的文件

Dim str() As String = IO.Directory.GetDirectories("F:\PC_Code")

For i = 0 To str.GetUpperBound(0)

'调用遍历过程

AddDirectory("F:\PC_Code:",node)

Next

node = Nothing

iDir = Nothing

End Sub

Function GetFiles(ByVal MyDir As String) As IEnumerable(Of System.IO.FileInfo)

Dim Coll As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
Coll = My.Computer.FileSystem.GetFiles("F:\PC_Code\1")
Dim filename As String
For Each filename In Coll
Dim node As New TreeNode
node.ImageIndex = 1
Dim arr As String() = filename.Split("\")

node.Text = arr(arr.Length - 1)

TreeView1.Nodes.Add(node)

Next

If (Not System.IO.Directory.Exists(MyDir)) Then
Throw New System.IO.DirectoryNotFoundException()
End If
Dim MyFileNames As String()
Dim MyFiles As List(Of System.IO.FileInfo) = New List(Of System.IO.FileInfo)()

MyFileNames = System.IO.Directory.GetFiles(MyDir)
For Each MyName As String In MyFileNames
MyFiles.Add(New System.IO.FileInfo(MyName))
Next
Return MyFiles
End Function

Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click
Dim MyDir As String = "F:\PC_Code\1"
Dim list As List(Of String) = New List(Of String)
Dim MyFiles = GetFiles(MyDir)

Dim MyStartDate As DateTime = Me.dateTimePicker1.Value
Dim MyEndDate As DateTime = Me.dateTimePicker2.Value

End Sub End Class

原文链接:https://www.f2er.com/vb/260012.html

猜你在找的VB相关文章