Recursive directory listing using WinForms TreeView?

I want to create a tree structure that displays all the folders in the system and display only music files such as .mp3.aiff.wav, etc.

I remember reading that I need to use a recursive function or something like that.

+5
source share
2 answers

Typically, most computers have thousands of folders and hundreds of thousands of files, so display them all in TreeView recursively with very slow and consume a lot of memory, view my answer to this question , referring to my answer with some changes, when you can get a pretty convenient graphical interface:

// Handle the BeforeExpand event
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
   if (e.Node.Tag != null) {
       AddDirectoriesAndMusicFiles(e.Node, (string)e.Node.Tag);
   }
}

private void AddDirectoriesAndMusicFiles(TreeNode node, string path)
{
    node.Nodes.Clear(); // clear dummy node if exists

    try {
        DirectoryInfo currentDir = new DirectoryInfo(path);
        DirectoryInfo[] subdirs = currentDir.GetDirectories();

        foreach (DirectoryInfo subdir in subdirs) {
            TreeNode child = new TreeNode(subdir.Name);
            child.Tag = subdir.FullName; // save full path in tag
            // TODO: Use some image for the node to show its a music file

            child.Nodes.Add(new TreeNode()); // add dummy node to allow expansion
            node.Nodes.Add(child);
        }

        List<FileInfo> files = new List<FileInfo>();
        files.AddRange(currentDir.GetFiles("*.mp3"));
        files.AddRange(currentDir.GetFiles("*.aiff"));
        files.AddRange(currentDir.GetFiles("*.wav")); // etc

        foreach (FileInfo file in files) {
            TreeNode child = new TreeNode(file.Name);
            // TODO: Use some image for the node to show its a music file

            child.Tag = file; // save full path for later use
            node.Nodes.Add(child);
        }

    } catch { // try to handle use each exception separately
    } finally {
        node.Tag = null; // clear tag
    }
}

private void MainForm_Load(object sender, EventArgs e)
{
    foreach (DriveInfo d in DriveInfo.GetDrives()) {
        TreeNode root = new TreeNode(d.Name);
        root.Tag = d.Name; // for later reference
        // TODO: Use Drive image for node

        root.Nodes.Add(new TreeNode()); // add dummy node to allow expansion
        treeView1.Nodes.Add(root);
    }
}
+14
source

Recursively searching all drives for specific files will not work. It takes about a minute to do this with today's large disks.

, Windows, , . node node. node ( BeforeExpand), node , . node . Etcetera.

, . node + . , Explorer , , , node. + .

, . . , , . , , .

, Windows . . Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), . .

+5

All Articles