In a word: Recursion.
If you haven't met it before it is a simple idea, that is best explained by:
Recursion: see "Recursion"
Google for it, and read up on what it says.
The basic idea is:
private void button1_Click(object sender, EventArgs e)
{
LookAtDir(@"F:\Temp");
}
private void LookAtDir(string path)
{
foreach (string dir in Directory.GetDirectories(path))
{
LookAtDir(dir);
}
...Do something with the directory.
}
Private Sub button1_Click(sender As Object, e As EventArgs)
LookAtDir("F:\Temp")
End Sub
Private Sub LookAtDir(path As String)
For Each dir As String In Directory.GetDirectories(path)
LookAtDir(dir)
Next
End Sub
[edit]Sorry - I forgot you were VB - OriginalGriff[/edit]
"Ok I have added this to my treeview and all seems well. I had to add a try catch statement to your private sub LookAtDir but now I am unsure it is doing anything at all. Can you tell me how to itterate through the folders and subfolders to show the current folder scanned in a textbox? so that i can see this in action.........."
This can get complex: Bear in mind that it will slow things down LOTS to show each of your folders and sub-folders as you process it, and unless you move the processing code to a background thread or do something nasty you won't see anything happening on screen.
To move it to a background thread, search the articles on this site, I know there are a couple of very good ones: I think Luc Pattyn, Pete O'Hanlon, and DavyM69 will be a good place to start, but it's been a while since I looked at them.
To do the nasty, call the Application.DoEvents method after every update to your TextBox. Nasty, and will slow things down a lot, but it'll be ok for testing.
Alternatively, for testing output the current path to the console...
"Ok once again sorry for my ignorance but will you show me in writing what you mean by doing the nasty? lol......... I am new to console and some of the terms you have used here. also I would love it if you could explain the part of your code that points to the dir.... LookAtDir("F:\Temp"). How can I enable the user to click any root dir or folder/subfolder and make it available to scan?.... I know that theese are not easy questions and know that I may not be asking the correct questions but I think you get the idea where id like to go with this..... In short I want a treeview that can show all hard drives and there folders/subfolders. The user then clicks the desired root such as C:\ or D:\ ect and clicks start........ The textbox then shows the current folder being scanned untill eof...."
Doing it the nasty way, instead of the "...Do something with the directory." bit above:
textBox1.Text += dir
Application.DoEvents()
It is nasty though, and should only be for testing. Threading is a much better solution for the real world.
Console: If you create a console app, you can do Console.WriteLine to print to the "screen", yes? It works in WinForms apps as well - the output goes to the "output" pane of the debugger...
All the routine LookAtDir needs is the text path to the directory. So if you pass it "F:\" (the root of the 'F' drive), it will scan it and call itself with a new path for each folder within the root drive of 'F'. So if you have the following structure:
F:\
Temp
Folder1
Perm
Folder2
Folder3
then LookAtDir will be called with:
LookAtDir("F:\")
LookAtDir("F:\Temp")
LookAtDir("F:\Temp\Folder1")
LookAtDir("F:\Perm")
LookAtDir("F:\Perm\Folder2")
LookAtDir("F:\Perm\Folder3")
So all you have to do is pass the path to the top level folder you want to scan, and it will handle all the ones below. If you called
LookAtDir("F:\Perm")
then it would just process
LookAtDir("F:\Perm")
LookAtDir("F:\Perm\Folder2")
LookAtDir("F:\Perm\Folder3")
and ignore all the others.