Results 1 to 12 of 12

Thread: Show folders/files in TreeView

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,849

    Show folders/files in TreeView

    Does anyone have a code snippet they could post that will take a specified folder & show the files & subfolders hierarchy in a TreeView? I did a quick search but I couldn't what I was looking for. Thanks...

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,292

    Re: Show folders/files in TreeView

    Add a treeview and a button to your form then try this:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         'Get a list of drives
    3.         Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
    4.         Dim rootDir As String = String.Empty
    5.         'Now loop thru each drive and populate the treeview
    6.         For i As Integer = 0 To drives.Count - 1
    7.             rootDir = drives(i).Name
    8.             'Add this drive as a root node
    9.             TreeView1.Nodes.Add(rootDir)
    10.             'Populate this root node
    11.             PopulateTreeView(rootDir, TreeView1.Nodes(i))
    12.         Next
    13.     End Sub
    14.  
    15.     Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
    16.         Dim folder As String = String.Empty
    17.         Try
    18.             Dim folders() As String = IO.Directory.GetDirectories(dir)
    19.             If folders.Length <> 0 Then
    20.                 Dim childNode As TreeNode = Nothing
    21.                 For Each folder In folders
    22.                     childNode = New TreeNode(folder)
    23.                     parentNode.Nodes.Add(childNode)
    24.                     PopulateTreeView(folder, childNode)
    25.                 Next
    26.             End If
    27.         Catch ex As UnauthorizedAccessException
    28.             parentNode.Nodes.Add(folder & ": Access Denied")
    29.         End Try
    30.     End Sub

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,849

    Re: Show folders/files in TreeView

    Nice code Stanav ... thanks.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,849

    Re: Show folders/files in TreeView

    Stanav ... can you help me with one more thing? Your code shows just the folders & subfolders. I want to show the files also, that are in the folders. I tried doing this myself, but I ended up with a big mess. Can you show me how to add the files to the TreeView also? I know how to get all the files in a folder, I'm just not sure where to add the code in the loop.

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,292

    Re: Show folders/files in TreeView

    Try this now. Note that on the tree nodes, I only display the folder name or the file name. The full path is stored in the node.Tag property should you need it later. Just remember, since the Tag property returns an Object, you have to cast it back to String whenever you read the Tag property of a node.
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         'Get a list of drives
    3.         Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
    4.         Dim rootDir As String = String.Empty
    5.         'Now loop thru each drive and populate the treeview
    6.         For i As Integer = 0 To drives.Count - 1
    7.             rootDir = drives(i).Name
    8.             'Add this drive as a root node
    9.             Dim root As TreeNode = TreeView1.Nodes.Add(rootDir)
    10.             root.Tag = rootDir
    11.             'Populate this root node
    12.             PopulateTreeView(rootDir, TreeView1.Nodes(i))
    13.  
    14.         Next
    15.  
    16.     End Sub
    17.  
    18.     Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
    19.         Dim folder As String = String.Empty
    20.         Try
    21.             'Add the files to treeview
    22.             Dim files() As String = IO.Directory.GetFiles(dir)
    23.             If files.Length <> 0 Then
    24.                 Dim fileNode As TreeNode = Nothing
    25.                 For Each file As String In files
    26.                     fileNode = parentNode.Nodes.Add(IO.Path.GetFileName(file))
    27.                     fileNode.Tag = file
    28.                 Next
    29.             End If
    30.             'Add folders to treeview
    31.             Dim folders() As String = IO.Directory.GetDirectories(dir)
    32.             If folders.Length <> 0 Then
    33.                 Dim folderNode As TreeNode = Nothing
    34.                 Dim folderName As String = String.Empty
    35.                 For Each folder In folders
    36.                     folderName = IO.Path.GetFileName(folder)
    37.                     folderNode = parentNode.Nodes.Add(folderName)
    38.                     folderNode.Tag = folder
    39.                     PopulateTreeView(folder, folderNode)
    40.                 Next
    41.             End If    
    42.         Catch ex As UnauthorizedAccessException
    43.             parentNode.Nodes.Add("Access Denied")
    44.         End Try
    45.     End Sub

  6. #6
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    Re: Show folders/files in TreeView

    If you want to convert it, you can use the load-on-demand treeview attached. The project is a VS2005 C# project that was converted to VS2008 C#, but you can just cut and paste the code into one of the many online C# to VB.NET converters.

    It loads folders and files, displays a folder/file icon and a checkbox on each node that selects all children. The sub-folders and files are only loaded when you click the plus (+), that way it's faster than recursing all folders/files at once.

    CT
    Attached Files Attached Files

  7. #7
    New Member
    Join Date
    May 2008
    Posts
    1

    Re: Show folders/files in TreeView

    Love you for this, i was trying to learn how to build complex tree structure that goes up to n times.

    i found several articles like load tree from XML, datatable blah..blah.. and none could be useful to me.

    but your article made me to understand better about treeview


    Thank you very much

  8. #8
    New Member
    Join Date
    Jun 2008
    Posts
    4

    Re: Show folders/files in TreeView

    Hello Stanav...

    Good Day!!!

    I get 3 error messages while using ur code.


    1) In the code "Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(of IO.DriveInfo) = My.Computer.FileSystem.Drives " there is a blue line below 'of' and while placing cursor below it says 'Array bounds cannot appear in type specifiers'.

    2) 'Treeview1.Nodes.Add(rootDir)', there is a line below 'rootDir' and it says 'Value of type string cannot be converted 'Microsoft.Web.UI.Webcontrols. TreeNode'.

    3) childNode = New TreeNode(folder). Here it says 'Too many argumenst to 'Public Sub New()'

    Please help me sort this out as I am very new to asp.net.

    Thanks and Regards,
    Reenna

  9. #9
    New Member
    Join Date
    Jun 2008
    Posts
    4

    Re: Show folders/files in TreeView

    Hi Stanav,

    Good Day!!!

    I get 3 errors while using ur code. Please help me solve them.

    1. Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) displays a line below 'Of' and says 'Array Bounds cannot appear in type specifiers'.

    2.Treeview1.Nodes.Add(rootDir) - line below 'rootDir' and says 'va;ue of type String cannot be converted to Microsoft.Web.UI.Webcontrol.TreeNode.

    3. childNode = New TreeNode(folder)..'Too many arguments to 'Public Sub New()'

    I am very new to asp.net. Please help me solve these...

    Regards,
    Reena

  10. #10
    New Member
    Join Date
    Jun 2008
    Posts
    4

    Re: Show folders/files in TreeView

    Hello Bhains Bhai...

    If you used the vb.net code by Stanav and not C#. Please help me with the same...I am using VS2003.

    Any help in this regard is highly appreciated.

    Thanks.
    Reena.

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,292

    Re: Show folders/files in TreeView

    Quote Originally Posted by rinjin
    Hello Bhains Bhai...

    If you used the vb.net code by Stanav and not C#. Please help me with the same...I am using VS2003.

    Any help in this regard is highly appreciated.

    Thanks.
    Reena.
    VS2003 is the culprit here. That code only works with VS2005 or VS2008.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  12. #12
    Junior Member
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    24

    Re: Show folders/files in TreeView

    Hi.

    I get a System.IO.IOException "The device is not ready."Are there any way to ignore this node and go to the next one ?

    by using something like this :

    Catch ex As System.IO.IOException
    With ex
    If .Message.Substring(0, 24) = "The device is not ready." Then

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