Files and Folders Treeview
Ola,
I have a code where I load a specific folder, but I also want to show the files within that specific folder. Anyone knows how?
code:
vb.net Code:
Public Sub LoadFolderTree(ByVal path As String)
Dim basenode As System.Windows.Forms.TreeNode
If IO.Directory.Exists(path) Then
If path.Length <= 3 Then
basenode = explorer_tree.Nodes.Add(path)
Else
basenode = explorer_tree.Nodes.Add(My.Computer.FileSystem.GetName(path))
End If
basenode.Tag = path
LoadDir(path, basenode)
Else
Throw New System.IO.DirectoryNotFoundException()
End If
End Sub
Private Sub explorer_tree_AfterExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles explorer_tree.AfterExpand
Dim n As System.Windows.Forms.TreeNode
For Each n In e.Node.Nodes
LoadDir(n.Tag, n)
Next
End Sub
Public Sub LoadDir(ByVal DirPath As String, ByVal Node As Windows.Forms.TreeNode)
On Error Resume Next
Dim Dir As String
Dim Index As Integer
If Node.Nodes.Count = 0 Then
For Each Dir In My.Computer.FileSystem.GetDirectories(DirPath)
Index = Dir.LastIndexOf("\")
Node.Nodes.Add(Dir.Substring(Index + 1, Dir.Length - Index - 1))
Node.LastNode.Tag = Dir
Node.LastNode.ImageIndex = 0
Next
End If
End Sub
Re: Files and Folders Treeview
vb Code:
Public Sub LoadDir(ByVal DirPath As String, ByVal Node As Windows.Forms.TreeNode)
On Error Resume Next
Dim Dir As String
Dim Index As Integer
If Node.Nodes.Count = 0 Then
For Each Dir In My.Computer.FileSystem.GetDirectories(DirPath)
Index = Dir.LastIndexOf("\")
Node.Nodes.Add(Dir.Substring(Index + 1, Dir.Length - Index - 1))
Node.LastNode.Tag = Dir
Node.LastNode.ImageIndex = 0
Next
For Each fi As IO.FileInfo In New IO.DirectoryInfo(DirPath).GetFiles
Node.Nodes.Add(fi.Name)
Next
End If
End Sub
Re: Files and Folders Treeview
Thanks Paul. Works like a charm and it seems that I finally could give you a +REP. ;)
Hey, here's another thing: How about getting the icons from windows to make it look a a windows explorer.
I have found an example, but I needed to create a custom control and this is much easier to handle.
example: http://glassocean.net/how-to-create-...ent-in-vb-net/
Re: Files and Folders Treeview
Here's an alternative, not as compact perhaps, but it fills the correspondent tree nodes only before they are selected which saves a lot of time when populating the treeview:
vb Code:
' tvFileSystem is a Treeview on this form
Imports System.IO
Public Class Form1
Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
For Each drive As IO.DriveInfo In My.Computer.FileSystem.Drives
Dim newroot As New TreeNode(drive.RootDirectory.Name)
newroot.Tag = drive
tvFileSystem.Nodes.Add(newroot)
Try
If drive.RootDirectory.GetDirectories().Length > 0 Then
newroot.Nodes.Add("")
newroot.Collapse()
End If
Catch ex As Exception
' swallow possible IO exceptions (i.e. drive not ready)
End Try
Next
End Sub
Private Sub FillNode(ByVal node As TreeNode)
Dim nodeinfo As IO.DirectoryInfo
Select Case True
Case TypeOf (node.Tag) Is IO.DriveInfo
nodeinfo = DirectCast(node.Tag, DriveInfo).RootDirectory
Case TypeOf (node.Tag) Is IO.DirectoryInfo
nodeinfo = DirectCast(node.Tag, DirectoryInfo)
Case Else
Exit Sub
End Select
Try
For Each di As DirectoryInfo In nodeinfo.GetDirectories()
Dim newsubitem As TreeNode = node.Nodes.Add(di.Name)
newsubitem.Tag = di
If di.GetDirectories().Length > 0 Then
newsubitem.Nodes.Add("")
newsubitem.Collapse()
End If
Next
For Each fi As FileInfo In nodeinfo.GetFiles()
Dim newsubitem As TreeNode = node.Nodes.Add(fi.Name)
newsubitem.Tag = fi
Next
Catch ex As Exception
' swallow possible io exceptions (i.e. Access denied)
End Try
End Sub
Private Sub tvFileSystem_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvFileSystem.BeforeExpand
If e.Node.Nodes.Count > 0 AndAlso e.Node.FirstNode.Text = "" Then
e.Node.Nodes.Clear()
FillNode(e.Node)
End If
End Sub
End Class
Re: Files and Folders Treeview
to get the associated icons for files:
vb Code:
Icon.ExtractAssociatedIcon(filename)
for drives + folders you need to use the API:
vb Code:
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_DISPLAYNAME = &H200
Private Const SHGFI_TYPENAME = &H400
Friend WithEvents img2 As System.Windows.Forms.ImageList
Private Const SHGFI_PIDL = &H8
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const CSIDL_DRIVES = &H11
Public Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String
End Structure
Private Declare Ansi 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 Declare Ansi Function SHGetFileInfo Lib "shell32.dll" _
(ByVal pszPath As IntPtr, _
ByVal dwFileAttributes As Integer, _
ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr
Public Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
(ByVal hwndOwner As Integer, ByVal nFolder As Integer, ByRef pidl As Integer) As Integer
vb Code:
Public Sub LoadDrives()
Dim nIndex As Integer = 1
Dim shinfo As SHFILEINFO
Dim drvs(), fi As String
drvs = IO.Directory.GetLogicalDrives()
shinfo = New SHFILEINFO()
Dim tmpPidl As IntPtr
SHGetSpecialFolderLocation(0, CSIDL_DRIVES, tmpPidl)
SHGetFileInfo(tmpPidl, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_PIDL + SHGFI_DISPLAYNAME + SHGFI_ICON + SHGFI_SMALLICON)
Marshal.FreeCoTaskMem(tmpPidl)
Dim myIcon As System.Drawing.Icon
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
img1.Images.Add(myIcon)
treeFiles.Nodes.Clear()
' Set the first node.
Dim rootNode As TreeNode = New TreeNode(shinfo.szDisplayName, 2, 2)
treeFiles.Nodes.Add(rootNode)
Dim drvCtr As Integer = 0
For Each fi In drvs
shinfo = New SHFILEINFO()
SHGetFileInfo(fi, FILE_ATTRIBUTE_NORMAL, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON + SHGFI_SMALLICON + SHGFI_DISPLAYNAME + SHGFI_TYPENAME)
myIcon = Nothing
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
img1.Images.Add(myIcon)
Dim n As TreeNode = New TreeNode(shinfo.szDisplayName, img1.Images.Count - 1, img1.Images.Count - 1)
rootNode.Nodes.Add(n)
Next
End Sub
Re: Files and Folders Treeview
Re: Files and Folders Treeview
Hmmm...
@Paul: can't get your example to work. I only get my drives of my laptop without any icons.
@Stanav: Great example, had seen that one before. I have to look into your code more, 'cause I want to change folder and filenames also and I think the Tag is the way to go.