How to Popular a Treeview with C#System Directories and Files

Asked

Viewed 622 times

1

In my application (Windows Forms) I have to make a tree of directories for the user to search for a (or more) file(s) within his system.

I saw on the Web how to make this tree in a Microsoft tutorial, but in their example Treeview only displays the directories, not the files themselves (the files are displayed in a Listview that is not practical).

The tree works, but I need to see the Directory files and if possible already select them (recover their Filename) for my application.

Follows the Code.

private void PopulateTreeView()
 {
     TreeNode rootNode;

     DirectoryInfo info = new DirectoryInfo(@"../..");
     if (info.Exists)
     {
         rootNode = new TreeNode(info.Name);
         rootNode.Tag = info;
         GetDirectories(info.GetDirectories(), rootNode);
         treeView1.Nodes.Add(rootNode);
     }
 }

 private void GetDirectories(DirectoryInfo[] subDirs,TreeNode nodeToAddTo)
 {
     TreeNode aNode;
     DirectoryInfo[] subSubDirs;
     foreach (DirectoryInfo subDir in subDirs)
     {
         aNode = new TreeNode(subDir.Name, 0, 0);
         aNode.Tag = subDir;
         aNode.ImageKey = "folder";
         subSubDirs = subDir.GetDirectories();
         if (subSubDirs.Length != 0)
         {
             GetDirectories(subSubDirs, aNode);
         }
         nodeToAddTo.Nodes.Add(aNode);
     }
 }

I saw in this tutorial that I can apply a foreach to each file found on Node and display it in Listview, but how to add it to Treeview ?

Thank you.

  • You can take a look at this video : https://www.youtube.com/watch?v=55PCdfvlyYk it shows how you can display items in the Tree View control in the specified directory. Once I’m on time I’ll try to post the answer.

  • Thanks friend, I can not watch video at work, but I will take a look at this link when I get home, thanks so much for the help

2 answers

1


I believe this example will help you.

File directory

inserir a descrição da imagem aqui

Code

 DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\mmurta\Desktop\PastaMeuSistema\Arquivos_Meu_Sistema");

        private void Form1_Load(object sender, EventArgs e)
        {
            if (Directory.Exists(@"C:\Users\mmurta\Desktop\PastaMeuSistema"))
            {
                try
                {
                    DirectoryInfo[] directories = directoryInfo.GetDirectories();

                    foreach (FileInfo file in directoryInfo.get())
                    {
                        if (file.Exists)
                        {
                            TreeNode nodes = treeView.Nodes[0].Nodes.Add(file.Name);
                        }
                    }

                    if (directories.Length > 0)
                    {
                        foreach (DirectoryInfo directory in directories)
                        {
                            TreeNode node = treeView.Nodes[0].Nodes.Add(directory.Name);
                            foreach (FileInfo file in directory.GetFiles())
                            {
                                if (file.Exists)
                                {
                                    TreeNode nodes = treeView.Nodes[0].Nodes[node.Index].Nodes.Add(file.Name);
                                }
                            }
                        }
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

Upshot :

inserir a descrição da imagem aqui

Then if you prefer you can download the código fonte here , was made available by the author of Video .

  • 1

    Opa friend, at the same time I was going to post the reply you sent to your HAHA, thanks for the help man.

  • Ok , Try to search the English terms in google on what you want to implement , if still the question hit us we are here =D. If you found suitable my example please check as reply*-*.

1

I managed to solve the problem, I made a logic so that inside each node where have files is created one more node to display them.

follows code:

    private void PopularTreeView()
    {
        TreeNode rootNode;

        DirectoryInfo info = new DirectoryInfo(@"D:\codigos");
        if (info.Exists)
        {
            rootNode = new TreeNode(info.Name);
            rootNode.Tag = info;
            GetDirectories(info.GetDirectories(), rootNode);
            treeView1.Nodes.Add(rootNode);
        }
    }

    private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
    {
        TreeNode aNode;
        DirectoryInfo[] subSubDirs;
        foreach (DirectoryInfo subDir in subDirs)
        {
            aNode = new TreeNode(subDir.Name, 0, 0);
            aNode.Tag = subDir;
            aNode.ImageKey = "icone-pasta";
            subSubDirs = subDir.GetDirectories();
            if (subSubDirs.Length != 0)
            {
                GetDirectories(subSubDirs, aNode);
            }
            nodeToAddTo.Nodes.Add(aNode);
            foreach (FileInfo file in subDir.GetFiles())
            {
                TreeNode node = new TreeNode();
                node.Text = file.Name;
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;
                nodeToAddTo.Nodes.Add(node);
            }
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.