Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hell there

 

I am presently writing an application called CDatabase which will scan a gived CDrom and store its logical information in a subtree of the main TreeView widget on the main form.

 

The problem lies in the fact that I cannot make my directory analyze function create nodes hierarchically using recursive functions.

 

Say I have a folder "Moo" on the CDrom. That folder has couple of files and some subfolders and so on. I can't create an in-memory image of that logical structure using TreeNodes, TreeView.

 

I actually figured a way of doing something, but it's not what I wanted. I scan the folders/files of the cdrom and place it all in an ArrayList. Then I process each item in the list and create nodes in the tree (I use String.split to get the nodes' names). But the problem is that my thing is not working properly. It would not create nodes/subnodes correctly and I don't know why... Here's what I have so far. It's not the way it should be, I know there is anoher solution to this problem. Ideas welcome!

 

 

Imports System.IO

 

 

Public Class DirAnalyzer

Dim entries As New ArrayList()

 

Sub New(ByVal tree As TreeView)

analyzeFolder("D:\New Folder")

populateTree(tree)

End Sub

 

Sub analyzeFolder(ByVal folder As String)

Dim subDirs As String() = Directory.GetDirectories(folder)

Dim files As String() = Directory.GetFiles(folder)

Dim file, dir As String

 

For Each file In files

entries.Add(file)

Next

 

For Each dir In subDirs

entries.Add(dir)

analyzeFolder(dir)

Next

End Sub

 

Sub populateTree(ByVal tree As TreeView)

Dim t As String

Dim a As Int32

Dim tokens() As String

 

For Each t In entries

Console.WriteLine("Line : " + t)

tokens = Split(t, "\")

While a < tokens.Length - 1

addItem(tokens(a), tree, a)

a = a + 1

End While

Next

End Sub

 

 

Sub addItem(ByVal text As String, ByRef tree As TreeView, ByVal level As Int32)

Console.WriteLine("adding entry: " + text)

Select Case level

Case 0

tree.Nodes.Add(text)

Case 1

tree.Nodes(0).Nodes.Add(text)

Case 2

tree.Nodes(0).Nodes(0).Nodes.Add(text)

Case 3

tree.Nodes(0).Nodes(0).Nodes(0).Nodes.Add(text)

Case 4

tree.Nodes(0).Nodes(0).Nodes(0).Nodes(0).Nodes.Add(text)

End Select

End Sub

End Class

 

 

===================

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...