Also note how the only thing being added anywhere (a tree node in this case) is the file locations. This behavior works on our example because we will be automatically creating the needed folders from the file path.
Creating the Nodes
The main objective in adding the node is creating a function that you can supply a string such as “C:Folder1Folder2file1.txt” and the proper nodes and child nodes will be created without duplicates.
Once again, we will employ our friend, Recursion. First, we must go back to theory. What keeps a recursive function from calling itself in an endless loop? The answer is a base case. The base case tells the function when it is okay to not call itself again.
In the code above, the base case is built in the subdirectory for-loop. When there are no subdirectories, the function does not call itself.
Back to the TreeView, our base case will be keeping track of the backslashes in the path. Once we have no more backslashes, we have reached the file part of the path.