aewarnick Posted March 2, 2003 Posted March 2, 2003 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? Quote C#
Leaders quwiltw Posted March 3, 2003 Leaders Posted March 3, 2003 I don't think so. I think you have drill down manually which should be pretty easy. Quote --tim
aewarnick Posted March 3, 2003 Author Posted March 3, 2003 It is easy to go down a set number of levels but I could not think of a universal method for any level. Can you think of any ideas of how to do it? Quote C#
Heiko Posted March 3, 2003 Posted March 3, 2003 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. Quote .nerd
aewarnick Posted March 3, 2003 Author Posted March 3, 2003 I thought that would be the way to do it but I have never used it before. I will have to do some testing and studying. Quote C#
Cywizz Posted March 3, 2003 Posted March 3, 2003 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 Quote Howzit??
Heiko Posted March 3, 2003 Posted March 3, 2003 And it works? (I'm asking coz you're passing the nodes byVal) Quote .nerd
*Gurus* divil Posted March 3, 2003 *Gurus* Posted March 3, 2003 Objects that are not value types are passed by reference regardless. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
CaNaDiAn BaCoN Posted March 3, 2003 Posted March 3, 2003 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 Quote
Leaders quwiltw Posted March 3, 2003 Leaders Posted March 3, 2003 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. Quote --tim
aewarnick Posted March 3, 2003 Author Posted March 3, 2003 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? Quote C#
Cywizz Posted March 4, 2003 Posted March 4, 2003 CaNaDiAn BaCoN, quwiltw...... It was a example on how recursion works, NOT production code. aewarnick, yes, the whole method is called Quote Howzit??
aewarnick Posted March 4, 2003 Author Posted March 4, 2003 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. Quote C#
Cywizz Posted March 4, 2003 Posted March 4, 2003 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. Quote Howzit??
aewarnick Posted March 4, 2003 Author Posted March 4, 2003 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. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.