Results 1 to 7 of 7

Thread: Files and Folders Treeview

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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:
    1. Public Sub LoadFolderTree(ByVal path As String)
    2.    Dim basenode As System.Windows.Forms.TreeNode
    3.    If IO.Directory.Exists(path) Then
    4.       If path.Length <= 3 Then
    5.          basenode = explorer_tree.Nodes.Add(path)
    6.       Else
    7.          basenode = explorer_tree.Nodes.Add(My.Computer.FileSystem.GetName(path))
    8.       End If
    9.       basenode.Tag = path
    10.       LoadDir(path, basenode)
    11.    Else
    12.       Throw New System.IO.DirectoryNotFoundException()
    13.    End If
    14. End Sub
    15.  
    16. Private Sub explorer_tree_AfterExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles explorer_tree.AfterExpand
    17.    Dim n As System.Windows.Forms.TreeNode
    18.    For Each n In e.Node.Nodes
    19.       LoadDir(n.Tag, n)
    20.    Next
    21. End Sub
    22.  
    23. Public Sub LoadDir(ByVal DirPath As String, ByVal Node As Windows.Forms.TreeNode)
    24.    On Error Resume Next
    25.    Dim Dir As String
    26.    Dim Index As Integer
    27.    If Node.Nodes.Count = 0 Then
    28.       For Each Dir In My.Computer.FileSystem.GetDirectories(DirPath)
    29.          Index = Dir.LastIndexOf("\")
    30.          Node.Nodes.Add(Dir.Substring(Index + 1, Dir.Length - Index - 1))
    31.          Node.LastNode.Tag = Dir
    32.          Node.LastNode.ImageIndex = 0
    33.       Next
    34.    End If
    35. End Sub


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Files and Folders Treeview

    vb Code:
    1. Public Sub LoadDir(ByVal DirPath As String, ByVal Node As Windows.Forms.TreeNode)
    2.     On Error Resume Next
    3.     Dim Dir As String
    4.     Dim Index As Integer
    5.     If Node.Nodes.Count = 0 Then
    6.         For Each Dir In My.Computer.FileSystem.GetDirectories(DirPath)
    7.             Index = Dir.LastIndexOf("\")
    8.             Node.Nodes.Add(Dir.Substring(Index + 1, Dir.Length - Index - 1))
    9.             Node.LastNode.Tag = Dir
    10.             Node.LastNode.ImageIndex = 0
    11.         Next
    12.         For Each fi As IO.FileInfo In New IO.DirectoryInfo(DirPath).GetFiles
    13.             Node.Nodes.Add(fi.Name)
    14.         Next
    15.     End If
    16. End Sub

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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/


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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:
    1. ' tvFileSystem is a Treeview on this form
    2. Imports System.IO
    3.  
    4. Public Class Form1
    5.     Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    6.         For Each drive As IO.DriveInfo In My.Computer.FileSystem.Drives
    7.             Dim newroot As New TreeNode(drive.RootDirectory.Name)
    8.             newroot.Tag = drive
    9.             tvFileSystem.Nodes.Add(newroot)
    10.  
    11.             Try
    12.                 If drive.RootDirectory.GetDirectories().Length > 0 Then
    13.                     newroot.Nodes.Add("")
    14.                     newroot.Collapse()
    15.                 End If
    16.             Catch ex As Exception
    17.                 ' swallow possible IO exceptions (i.e. drive not ready)
    18.             End Try
    19.         Next
    20.     End Sub
    21.  
    22.     Private Sub FillNode(ByVal node As TreeNode)
    23.         Dim nodeinfo As IO.DirectoryInfo
    24.         Select Case True
    25.             Case TypeOf (node.Tag) Is IO.DriveInfo
    26.                 nodeinfo = DirectCast(node.Tag, DriveInfo).RootDirectory
    27.             Case TypeOf (node.Tag) Is IO.DirectoryInfo
    28.                 nodeinfo = DirectCast(node.Tag, DirectoryInfo)
    29.             Case Else
    30.                 Exit Sub
    31.         End Select
    32.  
    33.         Try
    34.             For Each di As DirectoryInfo In nodeinfo.GetDirectories()
    35.                 Dim newsubitem As TreeNode = node.Nodes.Add(di.Name)
    36.                 newsubitem.Tag = di
    37.                 If di.GetDirectories().Length > 0 Then
    38.                     newsubitem.Nodes.Add("")
    39.                     newsubitem.Collapse()
    40.                 End If
    41.             Next
    42.  
    43.             For Each fi As FileInfo In nodeinfo.GetFiles()
    44.                 Dim newsubitem As TreeNode = node.Nodes.Add(fi.Name)
    45.                 newsubitem.Tag = fi
    46.             Next
    47.         Catch ex As Exception
    48.             ' swallow possible io exceptions (i.e. Access denied)
    49.         End Try
    50.     End Sub
    51.  
    52.     Private Sub tvFileSystem_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvFileSystem.BeforeExpand
    53.         If e.Node.Nodes.Count > 0 AndAlso e.Node.FirstNode.Text = "" Then
    54.             e.Node.Nodes.Clear()
    55.             FillNode(e.Node)
    56.         End If
    57.     End Sub
    58. End Class

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Files and Folders Treeview

    to get the associated icons for files:

    vb Code:
    1. Icon.ExtractAssociatedIcon(filename)

    for drives + folders you need to use the API:

    vb Code:
    1. Private Const SHGFI_ICON = &H100
    2. Private Const SHGFI_SMALLICON = &H1
    3. Private Const SHGFI_DISPLAYNAME = &H200
    4. Private Const SHGFI_TYPENAME = &H400
    5. Friend WithEvents img2 As System.Windows.Forms.ImageList
    6. Private Const SHGFI_PIDL = &H8
    7. Private Const FILE_ATTRIBUTE_NORMAL = &H80
    8.  
    9. Private Const CSIDL_DRIVES = &H11
    10.  
    11. Public Structure SHFILEINFO
    12.     Public hIcon As IntPtr
    13.     Public iIcon As Integer
    14.     Public dwAttributes As Integer
    15.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String
    16.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String
    17. End Structure
    18.  
    19. Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll" _
    20.         (ByVal pszPath As String, _
    21.          ByVal dwFileAttributes As Integer, _
    22.          ByRef psfi As SHFILEINFO, _
    23.          ByVal cbFileInfo As Integer, _
    24.          ByVal uFlags As Integer) As IntPtr
    25.  
    26. Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll" _
    27.         (ByVal pszPath As IntPtr, _
    28.          ByVal dwFileAttributes As Integer, _
    29.          ByRef psfi As SHFILEINFO, _
    30.          ByVal cbFileInfo As Integer, _
    31.          ByVal uFlags As Integer) As IntPtr
    32.  
    33. Public Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
    34. (ByVal hwndOwner As Integer, ByVal nFolder As Integer, ByRef pidl As Integer) As Integer

    vb Code:
    1. Public Sub LoadDrives()
    2.  
    3.     Dim nIndex As Integer = 1
    4.     Dim shinfo As SHFILEINFO
    5.     Dim drvs(), fi As String
    6.  
    7.     drvs = IO.Directory.GetLogicalDrives()
    8.  
    9.     shinfo = New SHFILEINFO()
    10.  
    11.     Dim tmpPidl As IntPtr
    12.     SHGetSpecialFolderLocation(0, CSIDL_DRIVES, tmpPidl)
    13.     SHGetFileInfo(tmpPidl, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_PIDL + SHGFI_DISPLAYNAME + SHGFI_ICON + SHGFI_SMALLICON)
    14.     Marshal.FreeCoTaskMem(tmpPidl)
    15.     Dim myIcon As System.Drawing.Icon
    16.     myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
    17.     img1.Images.Add(myIcon)
    18.  
    19.     treeFiles.Nodes.Clear()
    20.  
    21.     ' Set the first node.
    22.     Dim rootNode As TreeNode = New TreeNode(shinfo.szDisplayName, 2, 2)
    23.     treeFiles.Nodes.Add(rootNode)
    24.  
    25.     Dim drvCtr As Integer = 0
    26.  
    27.     For Each fi In drvs
    28.         shinfo = New SHFILEINFO()
    29.         SHGetFileInfo(fi, FILE_ATTRIBUTE_NORMAL, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON + SHGFI_SMALLICON + SHGFI_DISPLAYNAME + SHGFI_TYPENAME)
    30.         myIcon = Nothing
    31.         myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
    32.         img1.Images.Add(myIcon)
    33.  
    34.         Dim n As TreeNode = New TreeNode(shinfo.szDisplayName, img1.Images.Count - 1, img1.Images.Count - 1)
    35.         rootNode.Nodes.Add(n)
    36.  
    37.     Next
    38. End Sub

  6. #6

  7. #7

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width