Peruse a Adamantine Operate to a Tree in C
To scour down a difficult drive, in whatever programming language, the easiest way to advantage is recursion: a advantage calling itself. Everyone information and list then can be added to a TreeView (like in Windows Explorer). We testament extremely analyze how to add a way to a TreeView with C#.
To discover the insoluble drive, here is the pseudocode:
String[] array = move the directories in the root path
For each folder:
Call this function, with the advanced root path
String[] files = influence the files in the root path
For each file:
Add it to the tree control
Notice that the law is establish up so the consummation comes gone as
Folder
Folder
File
Much cognate Windows Explorer handles it. Nevertheless further indication that you don't corner to add it to a tree, you can achieve whatever you longing with the files.
As far as adding it to a TreeView control. The most efficient action to create it is by having a pathway "C:/Folder1/Folder2/file.txt" conceive the cold origin nodes and then make the youngster protuberance (the file).
Recursion is our boon companion here too, this interval in bullet points:
> Force up to the backslash with substring (C:, Folder1, Folder2).
> Test provided the root knot already exists, whether not beget it.
> Telephone this overhaul again, on the other hand with the adjacent "folder" and with a reference to the contemporary originator node
> Amass going until there are no added backslashes left, so we can fair-minded add the "file" node
The loveliness of recursion is how seemingly facile a servicing can look. Go after the conception outlined above and you will behold for yourself how efficiently it runs.
If you would allying to contemplate it already written outside in C#, download the project document here.
Published: March 21, 2008