ASP.NET Code
  Home arrow ASP.NET Code arrow Page 3 - Directory Tree Browser
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Developerworks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET CODE

Directory Tree Browser
By: Andrew_Putnam
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 85
    2003-06-28

    Table of Contents:
  • Directory Tree Browser
  • Getting Started
  • The Recursion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Directory Tree Browser - The Recursion
    (Page 3 of 3 )

    The key to making this all work properly is the folder recursion. The primary issue is when creating unique ID names used in the <div></div> tags so that the Javascript showing and hiding of the sub-folders works properly. This is accomplished by using a simple increasing integer counter that will be used as part of the ID name.

    In the example the starting folder for recursion is set in the constant "basePath". You can define this variable with any valid sub-folder under your web root. Do not use physical path names (e.g. c:\inetpub\www_root\images), because this routine uses Server.MapPath to grab the correct physical path from the virtual folder name (e.g. /images).

    Lastly, since you typically would not want to show any of the folders automatically created by FrontPage server extensions (if they are installed on your server), I include a boolean variable "hideFrontPage" with a default of True. If you want to show all folders including the ones created by FrontPage, set this value to False.


    tree.vb

     Imports Microsoft.VisualBasic Imports System Imports System.IO Imports System.Text Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.HtmlControls Public Class DirTree Inherits Page '-- define the base root path where the tree begins Private Const basePath As String = "/" '-- set to false if you want to show FrontPage '-- created folders Private Const hideFrontPage As Boolean = True '-- a folder counter used in creating path ID's Private divID As Integer = 0 '-- the base public tree builder Public Function buildTree() As String Dim tStr As New StringBuilder() tStr.Append("<INPUT onclick="" value=" Chr(34))        tStr.Append(Server.HtmlEncode(basePath) + Chr(34))        tStr.Append(" name="" np?? tStr.Append(?this.value)?? unlockSubmit(?) radio?

     <IMG src="" 0?? tStr.Append(?border="" ?) fld_open.gif?

    Root Folder " 
    basePath "
    "
    tStr.Append(recurseFolders(basePath10)) Return tStr.ToString End Function '-- used to recurse folders Private Function recurseFolders(ByVal startPath As String, _ ByVal folderLevel As Integer, _ ByVal rootID As Integer) As String '-- use a StringBuilder because its much '-- faster than standard string concatenation Dim folderStr As New StringBuilder() Dim actualFolder As String = Server.MapPath(startPath) Dim tFol As String = "" Dim si As New DirectoryInfo(actualFolder) Dim nf As String = "" Dim rp As String = "" '-- if folder doesnt exist, exit If si.Exists False Then Exit Function Else '-- begin recursion by geting a list of sub-folders in the directory Dim subFol() As String = Directory.GetDirectories(actualFolder) '-- get the folder count Dim folCount As Integer UBound(subFolDim fLoop As Integer '-- sort the folders by name Array.Sort(subFol) '-- if sub-folders exist If folCount >0 Then For fLoop LBound(subFolTo UBound(subFol'-- our unique ID number for the div ID's divID += If subFol(fLoopIs Nothing Then '-- skip it Else '-- print folder and recurse Dim sri As New DirectoryInfo(subFol(fLoop)) tFol "" If Right(actualFolder1) <>"\" Then actualFolder += "\" End If If Right(startPath1) <>"/" Then startPath += "/" End If nf startPath sri.Name rp nf.ToString '-- print check based on if we are hiding '-- frontpage created folders Dim doPrint As Boolean False If hideFrontPage True Then If isFrontPage(nf) = False Then doPrint True End If Else doPrint True End If '-- print folders if passed above test If doPrint = True Then tFol += recurseFolders(nf, folderLevel + 1, rootID) If tFol <>"" Then folderStr.Append(printSpacers(folderLevel)) folderStr.Append("<A href="" folderStr.Append(??);?? folderStr.Append(divID.ToString) ?_?) + folderLevel.ToString folderStr.Append(?_? folderStr.Append(rootID.ToString) ?f?) folderStr.Append(??, folderStr.Append(?,?i?) folderStr.Append(?_?) folderLevel.ToString) folderStr.Append(?d?) javascript:showHide(?)>") folderStr.Append("<IMG alt="" src="" name="" 0?? ?) ?_?) + folderStr.Append(?_? folderStr.Append(?folder?? this Expand folderStr.Append(? Chr(34)) divID.ToString folderStr.Append(folderLevel.ToString) folderStr.Append(rootID.ToString i?) folderStr.Append(?border="" plus.gif

    </A>") Else folderStr.Append(printSpacers(folderLevel)) folderStr.Append("<IMG height=1 src="" 0?? ?) folderStr.Append(?border="" 16?? folderStr.Append(?width="" 12?? transdot.gif?

    ") End If folderStr.Append("<INPUT value=")                            folderStr.Append(Chr(34))                            folderStr.Append(Server.HtmlEncode(rp) + Chr(34))                            folderStr.Append(" name="" np?? unlockSubmit(?) radio?? ?) folderStr.Append(?this.value)?? folderStr.Append(?onclick=""> <IMG src="" name="" ? 0?? ?) + folderStr.Append(?_? folderStr.Append(rootID.ToString) folderStr.Append(?_?) folderStr.Append(folderLevel.ToString) folderStr.Append(?border="" folderStr.Append(Chr(34) divID.ToString) f?) fld_close.gif?

    "" Then folderStr.Append("
    <DIV id="" ?_?) + folderLevel.ToString folderStr.Append(?_? folderStr.Append(rootID.ToString) folderStr.Append(? Chr(34)) folderStr.Append(Chr(34)) display:none;?) folderStr.Append(divID.ToString d?)>") folderStr.Append(tFol + "</DIV>") End If End If End If Next End If End If Return folderStr.ToString End Function '
    -- prints spacers for use with the folder recursion Private Function printSpacers(ByVal spaceCount As IntegerDim sp As String "" Dim i As Integer 0 i spaceCount 25 If spaceCount >0 Then sp "<IMG src="" width=" Chr(34) + i.ToString Chr(34) + " border=0 + transdot.gif?? ?12?? sp Chr(34) 0

    End If Return sp End Function '-- returns boolean if folder is one of the default FrontPage folder names Private Function isFrontPage(ByVal folderName As String) As Boolean Dim isFP As Boolean = False Select Case Right(folderName.ToLower.Trim, 8) Case "_private" isFP = True Case "_vti_cnf" isFP = True Case "_vti_log" isFP = True Case "_vti_pvt" isFP = True Case "i_script" isFP = True Case "_vti_txt" isFP = True Case Else isFP = False End Select Return isFP End Function End Class 

    I Hope you found this helpful. Comments can be sent to Andrew@dotNetBB.com.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway