How to Popular a TreeView with Directories and System Files in C #

1

In my application (Windows Forms) I have to make a directory tree for the user to search for one (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 the 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 (retrieve the FileName from them) for my application.

Follow 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 the node and display it in the ListView, but how do I add it to TreeView?

Thank you.

    
asked by anonymous 18.03.2016 / 15:27

2 answers

1

I believe this example will help you.

Files directory

Code

DirectoryInfodirectoryInfo=newDirectoryInfo(@"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);
                }
            }
        }

Result:

Then,ifyouprefer,youcandownloadthecódigofonte here , was made available by the author of Video .

    
18.03.2016 / 17:47
1

I was able to solve the problem, I made a logic so that within each node where it has files a more node is created to display them.

follow 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);
            }
        }
    }
    
18.03.2016 / 17:59