OK, I post the Code directly with no unnecessary words:
Code
<% @ Page Language = " C # " %>
<!Doctype HTML public"-// W3C // dtd xhtml 1.0 transitional // en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< Script runat = " Server " >
Protected Void Page_load ( Object Sender, eventargs E)
{
If (Page. ispostback = False )
{
System. Io. directoryinfo rootdir = New System. Io. directoryinfo (server. mappath ( " ~ / " ));
// output the Directory into a node
treenode rootnode = outputdirectory (rootdir, null );
// Add the output to the tree
treeview1.nodes. add (rootnode);
}< BR >}
treenode outputdirectory (system. io. directoryinfo directory, treenode parentnode)
{< br> /// validate Param
If (directory = null ) return null ;
// Create a node for this directory
treenode dirnode = New treenode (directory. name);
//Get subdirectories of the current directory
System. Io. directoryinfo [] subdirectories=Directory. getdirectories ();
// Output each subdirectory
For ( Int Directorycount = 0 ; Directorycount < Subdirectories. length; directorycount ++ )
{
Outputdirectory (subdirectories [directorycount], dirnode );
}
// output the current directories files
system. io. fileinfo [] files = directory. getfiles ();
For(IntFilecount= 0; Filecount<Files. length; filecount++)
{
Dirnode. childnodes. Add (NewTreenode (files [filecount]. Name ));
}
// If the parent node is null, return this node
// Otherwise add this node to the parent and return the parent
If (Parentnode = Null )
{
Return Dirnode;
}
Else
{
Parentnode. childnodes. Add (dirnode );
Return Parentnode;
}
}
</ Script >
< HTML xmlns = " Http://www.w3.org/1999/xhtml " >
< Head runat = " Server " >
< Title > Using Treeview to display a directory structure </ Title >
</ Head >
< Body >
< Form ID = " Form1 " Runat = " Server " >
< Div >
< ASP: Treeview ID = " Treeview1 " Runat = " Server " >
</ ASP: Treeview >
</ Div >
</ Form >
</ Body >
</ Html >