title: How to display all files under folders in treeview
body: <p>Iam trying to get all drives, folders, subfolders and files in treeview control using winforms. I have seen the following article.</p>
  
  <p><a href="http://codehill.com/2013/06/list-drives-and-folders-in-a-treeview-using-c/" rel="nofollow noreferrer">http://codehill.com/2013/06/list-drives-and-folders-in-a-treeview-using-c/</a></p>
  
  <p>but this only shows drives, folders and sub folders but not the files containing in these folders.</p>
  
  <p>Please anyone help and guide that how can i view all these files under these folders in treeview, thanks in advance.</p>
  
  <p>EDIT:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace GetDrives
  {
      public partial class Form2 : Form
      {
          public Form2()
          {
              InitializeComponent();
          }
  
          private void Form2_Load(object sender, EventArgs e)
          {
  
              //get a list of the drives
              string[] drives = Environment.GetLogicalDrives();
  
              foreach (string drive in drives)
              {
                  DriveInfo di = new DriveInfo(drive);
                  int driveImage;
  
                  switch (di.DriveType)    //set the drive's icon
                  {
                      case DriveType.CDRom:
                          driveImage = 3;
                          break;
                      case DriveType.Network:
                          driveImage = 6;
                          break;
                      case DriveType.NoRootDirectory:
                          driveImage = 8;
                          break;
                      case DriveType.Unknown:
                          driveImage = 8;
                          break;
                      default:
                          driveImage = 2;
                          break;
                  }
  
                  TreeNode node = new TreeNode(drive.Substring(0, 1), driveImage, driveImage);
                  node.Tag = drive;
  
                  if (di.IsReady == true)
                      node.Nodes.Add("...");
  
                  dirsTreeView.Nodes.Add(node);
              }
  
          }
  
          private void dirsTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
          {
              if (e.Node.Nodes.Count &gt; 0)
              {
                  if (e.Node.Nodes[0].Text == "..." &amp;&amp; e.Node.Nodes[0].Tag == null)
                  {
                      e.Node.Nodes.Clear();
  
                      //get the list of sub direcotires
                      string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());
  
                      foreach (string dir in dirs)
                      {
                          DirectoryInfo di = new DirectoryInfo(dir);
                          TreeNode node = new TreeNode(di.Name, 0, 1);
  
                          try
                          {
                              //keep the directory's full path in the tag for use later
                              node.Tag = dir;
  
                              //if the directory has sub directories add the place holder
                              if (di.GetDirectories().Count() &gt; 0)
                                  node.Nodes.Add(null, "...", 0, 0);
  
                              foreach (var file in di.GetFiles())
                              {
                                  TreeNode n = new TreeNode(file.Name, 13, 13);
                                  node.Nodes.Add(n);
                              }
  
                          }
                          catch (UnauthorizedAccessException)
                          {
                              //display a locked folder icon
                              node.ImageIndex = 12;
                              node.SelectedImageIndex = 12;
                          }
                          catch (Exception ex)
                          {
                              MessageBox.Show(ex.Message, "DirectoryLister",
                                  MessageBoxButtons.OK, MessageBoxIcon.Error);
                          }
                          finally
                          {
                              e.Node.Nodes.Add(node);
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I have now updated my code in application and using only one treeview, but the problem still exists.  You can see in image, in my C drive i have a file name "courses outline.html" and ab.txt which are not showing in application, which i need to see. Please see iamge below to easily understand my requirement.
  <a href="https://i.stack.imgur.com/l9zU0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l9zU0.png" alt="click on this image"></a></p>