I have written a code for populating the folder structure into tree view. But whenever the folder is not accesible its throwing an error or keeps waiting. I want it to skip that folder and move to next folder. Below is my code
Private Sub BuildDirectoryTree()
Dim newDirectory As TreeNode
Dim justTheSubdirectory As String
Dim basePath as String = "C:\"
For Each oneDirectory As String In My.Computer.FileSystem.GetDirectories(basePath)
justTheSubdirectory = My.Computer.FileSystem.GetName(oneDirectory)
If (fromNode Is Nothing) Then
newDirectory = FrmTreeViewDirectory.Nodes.Add(justTheSubdirectory)
Else
newDirectory = fromNode.Nodes.Add(justTheSubdirectory)
End If
BuildDirectoryTree(newDirectory, My.Computer.FileSystem.CombinePath(basePath, justTheSubdirectory))
Next oneDirectory
End Sub
Can any one modify this and post it back so that it works for the folders that its accessible
Loading

FroglegRank #1,020 · 1.9K reputationPosted Feb 26, 2011, 7:21 AM EST
Dim rootNode, tNode, tempNode As TreeNode
Private Sub BuildDirectoryTree()
Dim isRoot As String = ("C:\")
rootNode = FrmTreeViewDirectory.Nodes.Add(isRoot)
For Each dir As String In My.Computer.FileSystem.GetDirectories(isRoot)
addSubFolder(dir)
Next
End Sub
Private Sub addSubFolder(ByVal dir As String)
Try
tNode = rootNode.Nodes.Add(dir)
tempNode = tNode.Nodes.Add("temp") 'add temp node
Catch
End Try
End Sub
As a side note
When populating a treeview(TVw) with a directory structure
To make the program load quicker I usually load the top directories only
So the structure of the TVw will be root folder node,child sub directories nodes, and a temp node
When the user click on a sub directory node, then you build the corresponding nodes for that directory
Raghavendra Shetty13 reputationPosted Feb 26, 2011, 7:16 PM EST
this is working good. but ,
tNode = rootNode.Nodes.Add(dir)
this will each time give me the complete folder path , i just want the folder name.
FroglegRank #1,020 · 1.9K reputationPosted Feb 27, 2011, 9:12 PM EST
Dim isRoot As String = ("C:\")
rootNode = FrmTreeViewDirectory.Nodes.Add(isRoot)
rootNode.Tag = isRoot 'name the node
For Each dir As String In My.Computer.FileSystem.GetDirectories(isRoot)
addSubFolder(dir)
Next
End Sub
Public Function getFolder(ByVal str As String)
Dim i As Integer
Dim shrtPath As String
i = str.LastIndexOf("\")
shrtPath = str.Substring(i + 1)
Return shrtPath
End Function
Private Sub addSubFolder(ByVal dir As String)
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
End Try
End Sub
Raghavendra Shetty13 reputationPosted Mar 10, 2011, 1:45 AM EST
I appreciate your help,
but the code when clicked on the folder it does not add the folder to the parent folder i creates a seprate node at the end of the tree. I want it to add to the parent node. i.e. the sub folder should be added under the parent node.
FroglegRank #1,020 · 1.9K reputationPosted Mar 10, 2011, 2:39 AM EST
The minor problem is a plus sign showing where no sub directory exists.
Once you click the + if no sub directory, it disappears.
Raghavendra Shetty13 reputationPosted Mar 10, 2011, 2:58 AM EST
Now suppose i have the folder structure as below
folder1
subfolder1-1
subfolder1-2
subfolder1-3
folder2
subfolder2-1
subfolder2-2
folder3
subfolder3-1
subfolder3-2
Now suppose i click on "subfolder2-1" the folder formate should be
folder1
subfolder1-1
subfolder1-2
subfolder1-3
folder2
subfolder2-1
Subfolder2-1-1
subfolder2-2
folder3
subfolder3-1
subfolder3-2
But this is not happening
folder1
subfolder1-1
subfolder1-2
subfolder1-3
folder2
subfolder2-1
subfolder2-2
folder3
subfolder3-1
subfolder3-2
folder2
subfolder2-1
Subfolder2-1-1
subfolder2-2
Let me know how can this be resloved
FroglegRank #1,020 · 1.9K reputationPosted Mar 10, 2011, 3:52 AM EST
Raghavendra Shetty13 reputationPosted Mar 10, 2011, 8:13 AM EST
I am pasting the code below could you please let me know how i can append the parent node rather than creating a separate node. I am attaching the screen shot in this for your better understanding
Public
rootNode = FrmTreeViewDirectory.Nodes.Add(isRoot)
rootNode.Tag = isRoot
Class Form1Dim rootNode, tNode, tempNode As TreeNodePrivate Sub BuildDirectoryTree(ByVal path As String)Dim isRoot As String = path'name the nodeaddSubFolder(dir)
For Each dir As String In My.Computer.FileSystem.GetDirectories(isRoot)Next
End Sub
Public Function getFolder(ByVal str As String)Dim i As Integer
Dim shrtPath As Stringi = str.LastIndexOf(
shrtPath = str.Substring(i + 1)
"\")Return shrtPathEnd Function
Private Sub addSubFolder(ByVal dir As String)Try
Dim shrtPath As StringshrtPath = getFolder(dir)
tNode = rootNode.Nodes.Add(shrtPath)
tNode.Tag = dir
'name the nodetempNode = tNode.Nodes.Add(
"temp") 'add temp nodetempNode.Tag = (
"temp") 'name the node
Catch
End Try
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadDim path As String = "C:\"BuildDirectoryTree(path)
End SubBuildDirectoryTree(path)
End
Private Sub FrmTreeViewDirectory_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles FrmTreeViewDirectory.DoubleClickDim path As String = FrmTreeViewDirectory.SelectedNode.FullPath.ToStringEnd Sub ClassFroglegRank #1,020 · 1.9K reputationPosted Mar 10, 2011, 11:45 AM EST
To add
Node to FrmTreeViewDirectory use *** rootNode = FrmTreeViewDirectory.Nodes.Add(C: or dvd drive)
Node to C: node use *** tNode = rootNode.Nodes.Add(sub folders)
Node to tNode use *** subNode = tNode.Nodes.Add(lower folders)