Enumerating all Folders and Files (into a TreeView)

Today someone asked how they might enumerate all files and folders on a device.  The concept is pretty simple, and implementing it with a recursive algorithm is straightforward.  To make it a bit more useful I’m adding the items to a TreeView and saving the full path to the display file/folder in the TreeNode’s Tag property, so when a node is selected, you don’t have to reassemble the path.


public void GetListOfAllFilesAndFolders(TreeView view)
    TreeNode node = new TreeNode(“My Device”);

    AddFolderToTreeNode(“\”, node);

private void AddFolderToTreeNode(string rootPath, TreeNode rootNode)
    // add in all directories
    string[] dirList = Directory.GetDirectories(rootPath);

    foreach (string dir in dirList)
        TreeNode node = new TreeNode(Path.GetFileName(dir));
        node.Tag = dir;
        AddFolderToTreeNode((string)node.Tag, node);

    // add all files
    string[] fileList = System.IO.Directory.GetFiles(rootPath);

    foreach (string file in fileList)
        TreeNode node = new TreeNode(Path.GetFileName(file));
        node.Tag = file;



Yes, It’s that simple.

NOTE: I tested this on an OLDI 56SAM-400 800MHz x86 CE device running CE 5.0 so your mileage may vary.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s