The IO.Directory.GetDirectory method gets all subfolder paths in a folder and the GetFiles method gets all file paths with an optional filter. A little recursive function calling those two and you're done.
Note that Directory.GetFiles has an option for a recursive search but it will choke on the inaccessible folder it encounters. With your own recursive method you catch exceptions on inaccessible folders and continue.
Looks like its going to be a little harder than expected -.-
I would like to have a folder icon near all the folders/subfolders and a check box near all the files. But first things first i have to get everything listing properly. Haven't had time this morning to work on it.
I am messing about a little and i am getting the following error on my second loop.
Access to the path 'C:\System Volume Information' is denied.
Note that Directory.GetFiles has an option for a recursive search but it will choke on the inaccessible folder it encounters. With your own recursive method you catch exceptions on inaccessible folders and continue.
I made this class for you. I think it works, but I'm not exactly sure.
Code:
/// <summary>
/// Provides methods for recursively enumerating through all directories and files in the given directory and adds each item to a treeview.
/// </summary>
class DirectoryTree
{
private TreeView treeView;
private TreeNode currentDirectory, previousDirectory;
private DirectoryInfo directoryInfo;
/// <summary>
/// Initializes a new instance of the DirectoryTree class.
/// </summary>
/// <param name="treeView">The treeview that will have items added to it.</param>
/// <param name="directory">The initial directory to start recursively searching through.</param>
public DirectoryTree(TreeView treeView, string directory)
{
this.treeView = treeView;
this.currentDirectory = treeView.Nodes.Add(directory);
this.previousDirectory = currentDirectory;
this.directoryInfo = new DirectoryInfo(directory);
}
/// <summary>
/// Recursively enumerates and walks through each directory and file in the initial directory and adds each item to the treeview.
/// </summary>
public void WalkDirectory()
{
WalkDirectory(directoryInfo);
}
private void WalkDirectory(DirectoryInfo directory)
{
foreach (FileInfo file in directory.GetFiles())
{
if (file.Extension.ToLower() == ".png")
{
currentDirectory.Nodes.Add(Path.GetFileNameWithoutExtension(file.Name));
}
}
DirectoryInfo[] subDirectories = directory.GetDirectories();
foreach (DirectoryInfo subDirectory in subDirectories)
{
currentDirectory = currentDirectory.Nodes.Add(subDirectory.Name);
WalkDirectory(subDirectory);
currentDirectory = previousDirectory;
}
}
}
Personally I use a "load on demand" directory treeview.
It only populates child nodes when you expand a node, so no recursion is needed and it fast enough for my purposes. However, you may need more functionality than this.
Anyway, I have attached a sample project, hope I removed all the binaries, that demostrates this approach.
You stated that you need to show a folder next to each directory and a checkbox next to each file, so you'll have to modify my sample to do this as well as load the files in the tree, but this should be easy.