I have a structure of folders and files which I want to read in and display in a TreeView. Both the folders and files are objects in my C# Visual Studio environment.
A folder object contains:
- Collection of files
- Collection of folders
The top level of the structure is a folder. I start here with reading and use a foreach loop to get all the files inside this top level. Then I need to read in the collection of folders to see if the top folder contains other folders. And for every folder I need to do repeat the same process.
I can start at the top level of the structure and check if that folder contains folders but the problem is that I don't know how much layers the structure exists of. Folders can be nested in each other with a unlimited amount of levels. If I use foreach loops I have to nest them in each other but the amount of nested loops will determine how many layers I will read in.
I am looking for a dynamic solution to keep reading the structure untill I reach the last layer without needing to use unnecessary code.
Does anyone has an solution?
Image of the structure. All folders and files in the image can be accessed as objects.
Image of the code used to loop through the structure.