Imports System.Runtime.InteropServices
Imports System.IO
Public Class Form1
Dim rootNode, tNode, tempNode As TreeNode
Dim icoList As New List(Of Integer)
#Region " ICON DECLARATIONS"
Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
(ByVal pszPath As String, _
ByVal dwFileAttributes As Integer, _
ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0 ' Large icon
Private Const MAX_PATH = 260
Private nIndex = 0
Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Private Sub AddImages(ByVal strFileName As String)
Dim shInfo As SHFILEINFO
shInfo = New SHFILEINFO()
shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
shInfo.szTypeName = New String(vbNullChar, 80)
Dim hIcon As IntPtr
hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
Dim MyIcon As Drawing.Bitmap
MyIcon = Drawing.Icon.FromHandle(shInfo.hIcon).ToBitmap
ImageList1.Images.Add(MyIcon)
nIndex = nIndex + 1
End Sub
Sub getIcons(ByVal fnode As TreeNode, ByVal fname As String)
Dim ico As Icon = Icon.ExtractAssociatedIcon(fname)
If icoList.Contains(ico.Handle) Then ' if icolist already contains icon handle use index
For i As Integer = 0 To icoList.Count
If icoList(i) = ico.Handle Then
fnode.ImageIndex = i
fnode.SelectedImageIndex = i
End If
Next
Else ' add to imagalist and icoList
icoList.Add(ico.Handle)
ImageList1.Images.Add(ico)
fnode.ImageIndex = ImageList1.Images.Count - 1 ' ImageList1 starts at zero - so minus 1
fnode.SelectedImageIndex = ImageList1.Images.Count - 1
End If
End Sub
#End Region
#Region " PROJECT EXPLORER FILES / FOLDERS"
Public Function getFolder(ByVal str As String) 'shorten path
Dim i As Integer
Dim shrtPath As String
i = str.LastIndexOf("\")
shrtPath = str.Substring(i + 1)
Return shrtPath
End Function
Public Sub load_files_treeview()
Try
TreeView1.Nodes.Clear()
Dim isRoot As String = (ToolStripTextBox1.Text) ' << Path of the folder
rootNode = TreeView1.Nodes.Add(isRoot)
rootNode.Tag = isRoot ' name the node
For Each dir As String In My.Computer.FileSystem.GetDirectories(isRoot)
addSubFolder(dir)
Next
icoList.Add(1)
icoList.Add(2)
loadFiles(rootNode, isRoot)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub addSubFolder(ByVal dir As String)
Application.DoEvents()
Try
Dim shrtPath As String
shrtPath = getFolder(dir)
tNode = rootNode.Nodes.Add(shrtPath)
tNode.Tag = dir 'name the node
tempNode = tNode.Nodes.Add("temp") 'add temp node
tempNode.Tag = ("temp") 'name the node
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Sub loadFolders(ByVal Tnode As TreeNode, ByVal fPath As String)
Application.DoEvents()
For Each folderNode As String In Directory.GetDirectories(fPath)
Dim shortNode As String
shortNode = getFolder(folderNode) 'shorten path
Dim subNode As TreeNode = Tnode.Nodes.Add(shortNode)
subNode.Tag = folderNode
subNode.Nodes.Add("temp")
Next
loadFiles(Tnode, fPath)
End Sub
Sub loadFiles(ByVal fnode As TreeNode, ByVal dir As String)
Application.DoEvents()
For Each filesNode As String In Directory.GetFiles(dir)
Dim subFileNode As TreeNode = fnode.Nodes.Add(filesNode)
subFileNode.Tag = filesNode
subFileNode.Text = Path.GetFileName(filesNode)
getIcons(subFileNode, filesNode)
Next
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Try
If TreeView1.SelectedNode.Nodes.Count = 1 & TreeView1.SelectedNode.Nodes(0).Text = "temp" Then
TreeView1.SelectedNode.Nodes.Clear() 'if only temp node exists, delete it
Else
TreeView1.SelectedNode.Nodes.Clear() 'delete temp node
loadFolders(TreeView1.SelectedNode, TreeView1.SelectedNode.Tag.ToString)
End If
If TreeView1.SelectedNode.ToString.Contains(".") Then
getIcons(TreeView1.SelectedNode, TreeView1.SelectedNode.Tag)
End If
Catch
End Try
End Sub
Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
Try
If e.Node.Nodes.Count = 1 Then
If e.Node.Nodes(0).Text.ToString = "temp" Then
e.Node.Nodes.Clear()
loadFolders(e.Node, e.Node.Tag.ToString)
End If
End If
Catch
End Try
End Sub
#End Region
' ACTIONS "OK" > Load the files and (sub)folders
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
load_files_treeview()
End Sub
' ACTION "EXPAND" > Expand the treeview
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
TreeView1.ExpandAll()
End Sub
' ACTION "SORT" > Sort the files and folders asc.
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
TreeView1.Sort()
End Sub
' doubleclick a node and set the tag into the textbox
Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
TextBox1.AppendText(e.Node.Tag & vbNewLine)
End Sub
End Class