title: Populating a treeview with files in ASP.NET C#
body: <p>I have a treeview that I am trying to populate with folders and files. The treeview is populating the folders just fine but not the files. Here is my code:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
          {
  
              if (!IsPostBack)
              {
                  PopulateTree();
              }              
  
          }
  
  private void PopulateTree()
          {
              //Populate the tree based on the subfolders of the specified VirtualImageRoot
              var rootFolder = new DirectoryInfo(VirtualImageRoot);
              var root = AddNodeAndDescendents(rootFolder, null);
  
              //Add the root to the TreeView
              TreeView1.Nodes.Add(root);
          }
  
  private TreeNode AddNodeAndDescendents(DirectoryInfo folder, TreeNode parentNode)
          {
              //Add the TreeNode, displaying the folder's name and storing the full path to the folder as the value...
              string virtualFolderPath;
  
              if (parentNode == null)
              {
                  virtualFolderPath = VirtualImageRoot;
              }
              else
              {
                  virtualFolderPath = parentNode.Value + folder.Name + "/";
              }
  
              var node = new TreeNode(folder.Name, virtualFolderPath);
  
              //Recurse through this folder's subfolders
              var subFolders = folder.GetDirectories();
  
              foreach (DirectoryInfo subFolder in subFolders)
              {
                  var child = AddNodeAndDescendents(subFolder, node);
  
                  foreach (FileInfo file in subFolder.GetFiles())
                  {
  
                      var index = file.FullName.LastIndexOf(@"\", StringComparison.Ordinal);
                      var strname = file.FullName.Substring(index + 1);
                      var name = strname.Split('.');
  
                      var tn = new TreeNode();
                      if (name.Length &gt; 1 &amp;&amp; name[1].ToLower() == "bch")
                      {
                          tn = new TreeNode(name[0], file.FullName);
                      }
                      else
                      {
                          tn = new TreeNode(name[0], file.FullName);
                      }
                      child.ChildNodes.Add(tn);
                  }
                  node.ChildNodes.Add(child);
  
              }
              //Return the new TreeNode
              return node;
          }
  </code></pre>
  
  <p>Here is what my tree looks like:</p>
  
  <p><img src="https://i.stack.imgur.com/Ky3DK.png" alt="My Tree View: without files"></p>
  
  <p>Here is a picture of the files in the folder:</p>
  
  <p><img src="https://i.stack.imgur.com/SwXLY.png" alt="My files"></p>
  
  <p>I am trying just to show the files with the type .bch, along with the folders in my treeview. Can someone please tell me what I am doing wrong?</p>