Click here to Skip to main content
15,306,245 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Ok I will try once more to ask this question as best I can. I have seen other programs use a treeview to obtain a scanning directory or path. All im aiming to do is use a treeview to select a path to start my scan from. My form1 contains a treeview named Tv1, A label named lblmessage and a start button. I need to itterate or recrusively scan through the directory, not to add or find directories. I think past attempts to help me with this have explained how to add directories and subdirectories which is quite the oppsite of what i need. Will someone please give a solution step by step how to complete this task?

Point form what I need:

1) Working treeview with root,folders,subfolders,files if possible
2) start button that recrusivly scans and itterates the selected treeview node selected by the user
3) a label that displays the current folder,path,file being scanned.


thats it............

thank you in advance
Posted

1 solution

You said:

"I need to iterate or recursively scan through the directory, not to add or find directories."

On the contrary, in order to populate the treeview, you need to find directories.

The way you determine what folder to scan depends entirely on how you populated the tree. Did you store the entire path somewhere, or just the folder name? If you stored the entire path, all you have to do is parse the stored path down to the selected sub-folder. You can determine that by counting how many times you have to backup to the root node in the tree. If you just stored the folder name (or if you'd rather just do it this way, which is how I would feel about it), you have to backup in the tree and get the parent node name(s) until you reach the root node, and build the path backwards as you go.

I doubt you'll get step-by-step instructions because we don't know all of your requirements, nor the code that surrounds this requirement.
   
Comments
Dale 2012 3-Feb-11 6:23am
   
This is what i have so far please please help me in getting this to scan directories



Imports System.IO
Public Class form1

Private Shared Sub FillOneLevel(ByVal root As TreeNode, ByVal path As String)
Dim di As New DirectoryInfo(path)
Try
For Each subFolder As DirectoryInfo In di.GetDirectories()
Dim node As New TreeNode(subFolder.Name) With {.Tag = subFolder.FullName}
root.Nodes.Add(node)
Next subFolder
Catch ex As Exception
' do something
End Try
End Sub

Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim drives() As String = Environment.GetLogicalDrives()
For Each dr As String In drives
Dim node As New TreeNode(dr) With {.Tag = dr}
FillOneLevel(node, dr)
tv1.Nodes.Add(node)
Next dr
tv1.Nodes(0).Expand()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
LookAtDir(lblMessage.Text)
End Sub

Private Sub LookAtDir(ByVal path As String)
Try
For Each dir As String In Directory.GetDirectories(path)
LookAtDir(dir)
Next
'...Do something with the directory.
Catch ex As UnauthorizedAccessException
End Try
End Sub

Private Sub tv1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tv1.AfterSelect
lblMessage.Text = tv1.SelectedNode.Tag
End Sub
End Class
#realJSOP 3-Feb-11 7:28am
   
Put your code into your original question so we can actuially read it. Like I said, I'm not a VB coder, so I will be of little help unless this is VB.Net.
Dale 2012 3-Feb-11 16:22pm
   
If you are not a VB coder how will you help in any way and why is it that you cannot place my question with my code that is on the same page?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900