Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's an elementary sample that I created awhile ago when learning to play with the TreeView. I have now converted the code to VB.NET using an <a href="http://www.developerfusion.com/tools/convert/csharp-to-vb/" rel="nofollow noreferrer">online converter</a> for your benefit.</p> <p>It recursively walks the directory tree starting from the root of the virtual directory and creates nodes for each sub-directory or file encountered. I think this is exactly what you needed.</p> <p>For visual separation, I had used icons to differentiate files from folders (folder.gif and file.gif). You can remove that parameter if you want.</p> <p>Complete ASPX follows (You can paste it into a new page and it should run):</p> <hr> <pre><code>&lt;%@ Page Language="VB" %&gt; &lt;%@ Import Namespace="System.IO" %&gt; &lt;script runat="server"&gt; Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Page.IsPostBack Then Dim rootDir As New DirectoryInfo(Server.MapPath("~/")) ' Enter the RecurseNodes function to recursively walk the directory tree. Dim RootNode As TreeNode = RecurseNodes(rootDir) ' Add this Node hierarchy to the TreeNode control. Treeview1.Nodes.Add(RootNode) End If End Sub Private Function RecurseNodes(ByVal thisDir As DirectoryInfo) As TreeNode Dim thisDirNode As New TreeNode(thisDir.Name, Nothing, "Images/folder.gif") ' Get all the subdirectories in this Directory. Dim subDirs As DirectoryInfo() = thisDir.GetDirectories() For Each subDir As DirectoryInfo In subDirs thisDirNode.ChildNodes.Add(RecurseNodes(subDir)) Next ' Now get the files in this Directory. Dim files As FileInfo() = thisDir.GetFiles() For Each file As FileInfo In files Dim thisFileNode As New TreeNode(file.Name, Nothing, "Images/file.gif") thisDirNode.ChildNodes.Add(thisFileNode) Next Return thisDirNode End Function &lt;/script&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Untitled Page&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:treeview ID="Treeview1" runat="server"&gt;&lt;/asp:treeview&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload