SoFunction
Updated on 2025-03-07

How to display files using TreeView

This article describes the method of using TreeView to display files, which is a very practical technique. Share it for your reference. The specific implementation method is as follows:

Usually, TreeView is very common. Here we will explain in detail how to use TreeView to display files.

1. First add the TreeView control:

<asp:TreeView ID="driverInfoView" runat="server" ImageSet="XPFileExplorer" OnTreeNodePopulate="driverInfoView_TreeNodePopulate"> 
     
</asp:TreeView> 

2. When the page is loading, write the following code:

protected void Page_Load(object sender, EventArgs e) 
{ 
    foreach (DriveInfo driverInfo in ()) 
    { 
      TreeNode newNode = new TreeNode(); 
       = false; 
       = true; 
       = ; 
 
      if () 
      { 
         =  + "(" +  + ")"; 
      } 
      else 
      { 
         =  + "(Not ready yet)"; 
      } 
      (newNode); 
    } 
} 

3. Set the events of the TreeView node:

protected void driverInfoView_TreeNodePopulate(object sender, TreeNodeEventArgs e) 
{ 
    DirectoryInfo dictInfo = new DirectoryInfo(); 
 
    foreach (DirectoryInfo directory in ()) 
    { 
      TreeNode newNode = new TreeNode(); 
       = false; 
       = true; 
       = ; 
       = ; 
      (newNode); 
    } 
 
    foreach (FileInfo fileInfo in ()) 
    { 
      TreeNode newNode = new TreeNode(); 
       = ; 
       = ; 
      (newNode); 
    } 
}

I believe that the examples described in this article have certain reference value for everyone's programming.