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...
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.
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Get a list of drives
Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
Dim rootDir As String = String.Empty
'Now loop thru each drive and populate the treeview
For i As Integer = 0 To drives.Count - 1
rootDir = drives(i).Name
'Add this drive as a root node
Dim root As TreeNode = TreeView1.Nodes.Add(rootDir)
root.Tag = rootDir
'Populate this root node
PopulateTreeView(rootDir, TreeView1.Nodes(i))
Next
End Sub
Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
Dim folder As String = String.Empty
Try
'Add the files to treeview
Dim files() As String = IO.Directory.GetFiles(dir)
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.
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.
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...