Using Treeview to display a directory structure

Source: Internet
Author: User

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 >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.