Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
All that I have seen are methods to get directory info and it's immediate subdirectories but what about the other subdirectories beneath? Is there a method to get all the levels of subdirectories from a parent dir?
C#
Posted

It's called recursion....

 

 

Sub myDirs (topLevel as directory, foundSoFar as collection)

 

for each dir in topLevel

foundSoFar.Add dir

Call myDirs(dir)

next Dir

 

 

End sub

 

Pseudocode, but I hop you get the idea.

.nerd
Posted

example on how to add it to, eg a treeview, when entering a path into a textbox and clicking on a button:

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
       TreeView1.Nodes.Clear()

       ProcessDirectories("c:\path....", TreeView1.Nodes)

End Sub

Private Sub ProcessDirectories(ByVal path As String, ByVal parentCollection As TreeNodeCollection)
       Dim items As String() = IO.Directory.GetDirectories(path)
       Dim item As String
       Dim itemNode As TreeNode
       For Each item In items
           itemNode = parentCollection.Add(item)
          'this is the recursion
           ProcessDirectories(item, itemNode.Nodes)
       Next

End Sub

I know its in vb, but i'm sure you can get to a translation

Howzit??
Posted

That may take a long time

 

I tried that on an old app, and I took forever to load the TreeView at runtime. You need to consider that people may have HUGE directory structures. I think you can do it faster by working with the Windows Shell but I am not sure

  • Leaders
Posted
I agree. If you *are* doing this to put in a tree control or something you should just expand it on an as-needed basis. via the BeforeExpand event for example.
--tim
Posted

recursion is very confusing. It looked logical at first but when I put in this code, it works and I have no idea why.

 

public static string[]GetAllFiles(string Dir)
	{
		DirectoryInfo Di=new DirectoryInfo(Dir);
		FileInfo[]FArr=Di.GetFiles();
		
		for(int j=0; j<FArr.Length; j++)
		{
			o+=FArr[j].FullName+"\n";
		}
		string[]SubDirs=Directory.GetDirectories(Dir);
		foreach(string D in SubDirs)
		{
			o+="1\n";
			GetAllFiles(D);
		}

		char[]sp=new Char[]{'\n'};
		string[]AllDirectories=o.Split(sp[0]);
		o="";
                                               return AllDirectories;
	}

 

I need to clear static string o but I cannot put it in the code anywhere because it erases too early and that is one thing I don't understand.

 

Does a recursive call do the entire method each time it is called?

C#
Posted

CaNaDiAn BaCoN, quwiltw......

It was a example on how recursion works, NOT production code.

 

aewarnick, yes, the whole method is called

Howzit??
Posted

Yep. I understand it now after playing with it for a while I see how it works. But it pretty much locks up when I try to get windows. I left for over an hour and it still was not done. Do you think that is normal or is my code just extremely inefficient?

 

I wanted a way to immediately put it into an array instead of a huge string but I could not see any way to do it because I don't know how many files are in all those directories so that I can size the array properly.

 

Also, when the computer is processing it it barely works! Is there any way to make it really work hard to speed the process up? Maybe putting the method on a separate thread? My other post is about that.

C#
Posted
What do you want to use this for? If you are creating some sort of 'windows explorer' like application, you must go with the design quwiltw suggested (expand on demand), and not load everyting at once.
Howzit??
Posted
Nope, it is not an explorer like inteface at all, the user will not even know what is going on, it will run about every 2 minutes and open files to read them and add to them a certain mark when the time is right. It is a security thing. So I need them all at once. It will not be near as long as windows but I think it still needs to be faster.
C#

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...