Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to show folder structure using TreeView control in Windows Form Application using C#.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.20k
    Comment on it

    Hello All,

    Working with Windows form application using C# while developing desktop application, many times we need to show the folder structure in TreeView control, and to do that we have the following code :


    private void btnShow(object sender, EventArgs e)
    {
      DirectoryInfo dirInfo = new DirectoryInfo(" Your Path of the root folder goes here ");
      if (dirInfo.Exists)
      {
        TreeNode mainNode = new TreeNode(txtPath.Text);
        fileViewerTreeView.Nodes.Add(mainNode);
        AddChildNodes(mainNode, " Your Path of the root folder goes here ");
      }
    }
    


    Here on a button click, we will add a root node to the TreeView control called "fileViewerTreeView" and than to add all the folders and file which exists inside the

    folder, we will call the function called "AddChildNodes" and will pass pass root node, and also the path of the folder, Now this "AddChildNodes" function is a recursive function which will recursively search of all the files and folders under the given path of the folder.


    private void AddChildNodes(TreeNode rootNode, string rootFoder)
    {
       try
       {
          DirectoryInfo dirs = new DirectoryInfo(rootFoder);
          foreach (DirectoryInfo dirInfo in dirs.GetDirectories())
          {
             TreeNode newnode = new TreeNode(dirInfo.Name);
              rootNode.Nodes.Add(newnode);
                        FileInfo[] nestedFiles = null;
                        nestedFiles = dirInfo.GetFiles().ToArray();
                        if (nestedFiles != null && nestedFiles.Length > 0)
                        {
                            foreach (FileInfo nestedFile in nestedFiles)
                            {
                                newnode.Nodes.Add(nestedFile.Name.ToString());
                            }
                        }
                        if (dirInfo.GetDirectories().Length > 0)
                        {
                            AddChildNodes(newnode, dirInfo.FullName);
                        }
                    }
                    FileInfo[] files = dirs.GetFiles(fileType);
                    foreach (FileInfo file in files)
                    {
                        rootNode.Nodes.Add(file.Name.ToString());
                    }
                    fileViewerTreeView.ExpandAll();
                    fileViewerTreeView.CheckBoxes = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
    }
    

    Output of the execution will be reflected on UI like below :

    alt text

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: