Click here to Skip to main content
15,867,141 members
Articles / Web Development / ASP.NET

Folder Contents DataSource Control

Rate me:
Please Sign up or sign in to vote.
4.24/5 (14 votes)
20 Jan 2007LGPL33 min read 91.9K   718   49   32
This is a data source control that lists the contents of a folder on the server. It's great for making file managers and photo galleries.

Sample Image - screenshot.gif

Introduction

In ASP.NET 2.0, accessing data in web pages is handled through data source controls. These are non-visual controls derived from DataSourceControl. Common examples are the SqlDataSource and the AccessDataSource.

Some time ago, I wanted to make a simple file manager. For this, I needed to list the contents of a folder. I decided to create a custom data source control, so that I could use a GridView control to display the data.

In combination with a GridView, the control can not only show the contents of a folder, but it can also be used to rename or delete files or folders! Therefore, this can be a great control to create your own file manager.

The control can also be used to create a photo gallery with almost no code, as shown below:

Image 2

The code is in VB.NET, but it's not difficult to get it running in C# or in another language.

License

The control is free and Open Source under the LGPL license.

Installation

Installing the control

You can install the control in the standard way:

  • Create a "bin" folder inside the application root of your website (if it's not already there)
  • Copy the assembly file foldercontentsdatasource.dll into the bin folder

You may want to add the control to the toolbox of your editor (Visual Studio or C# Builder). This will allow you to add the control to a page by dragging it. Follow the editor's procedure to add a control to the toolbox.

Using the control

Adding the control to your page

There are two ways to add the control to your page:

  1. If the control was installed on the toolbox, add a data control to the page first, such as a GridView, a DataList, or a Repeater. Next, use the Data Source Configuration Wizard to choose a data source. Make sure there's a copy of the evenlogdatasource.dll assembly in the bin folder.
  2. Image 3

  3. Add the code manually. Add this line to the top of your page:
  4. ASP.NET
    <%@ Register TagPrefix="rw" 
       Namespace="rw" Assembly="foldercontentsdatasourcecontrol" %>

    Then, add a tag like this to your page:

    ASP.NET
    <rw:FolderContentsDataSource 
         id="FolderContentsDataSource1" runat="server" >
    </rw:FolderContentsDataSource>

    Finally, add the data control (GridView or DataList) to the page, and connect it to the FolderContentsDataSource.

Setting the folder

The control will show the contents of the folder that is set as a Parameter. The name of the parameter is "Directory".

HTML
<rw:FolderContentsDataSource ID="FolderContentsDataSource1" runat="server" >
   <SelectParameters>
      <asp:Parameter Name="Directory" Type="String" DefaultValue="c:\inetpub\wwwroot" />
   </SelectParameters>
</rw:FolderContentsDataSource>

You can use any of the Parameter types, such as ControlParameter or QueryStringParameter, to connect the parameter value to an environment value. Use the Parameters setting in the Properties window. Please note that you only need a single parameter. This parameter is used for selection, deletion, and renaming of contents.

Demo pages

The control comes with two demo pages:

  • demo.aspx shows a list of all the files and subfolders in a selected folder. This page uses a GridView control, and a TextBox control for selecting a folder. The Directory parameter is set through a ControlParameter, which connects the TextBox to the data source.
  • demogallery.aspx shows a gallery with all the pictures in a given folder. This page uses a DataList control bound to the FolderContentsDatasource control. Use the RepeatColumns property of the DataList to control the number of pictures in a row.

Important: you need to run these pages with credentials that have read rights to the folder. For deleting or renaming files, you need the corresponding permissions. Use impersonation, or make sure the folder is accessible for the ASPNET account.

How it works

To build a data source control, I got some ideas from Nikhil Kothari's code at www.nikhilk.net/DataSourceControlsSummary.aspx. I built two main classes: FolderContentsDataSource and FolderContentsDataSourceView.

The code reading the folder contents is in the ExecuteSelect method of the FolderContentsDataSourceView class. This code generates a DataTable with a list of entries in the given folder:

VB
Protected Overrides Function ExecuteSelect(ByVal arguments _
         As System.Web.UI.DataSourceSelectArguments) _
         As System.Collections.IEnumerable
    Dim dt As New DataTable()
    dt.Columns.Add("Name", System.Type.GetType("System.String"))
    dt.Columns.Add("Size", System.Type.GetType("System.Int64"))
    dt.Columns.Add("Modified", System.Type.GetType("System.DateTime"))
    dt.Columns.Add("Created", System.Type.GetType("System.DateTime"))
    dt.Columns.Add("Accessed", System.Type.GetType("System.DateTime"))
    dt.Columns.Add("IsFolder", System.Type.GetType("System.Boolean"))

    Dim exc As Exception = Nothing
    Dim dv As DataView = Nothing
    Try
        Dim objFolderContents As DirectoryInfo = _
              New DirectoryInfo(_owner.GetSelectedDirectory())
        Dim objEntries() As FileSystemInfo = _
               objFolderContents.GetFileSystemInfos()
        Dim objEntry As FileSystemInfo
        For Each objEntry In objEntries
            Dim dr As DataRow = dt.NewRow()
            dr("Name") = objEntry.Name
            If (objEntry.Attributes And FileAttributes.Directory) <> 0 Then
                dr("Size") = 0
                dr("IsFolder") = True
            Else
                dr("Size") = CType(objEntry, FileInfo).Length
                dr("IsFolder") = False
            End If
            dr("Modified") = objEntry.LastWriteTime
            dr("Created") = objEntry.CreationTime
            dr("Accessed") = objEntry.LastAccessTime
            dt.Rows.Add(dr)
        Next
        dv = New DataView(dt)
        dv.Sort = arguments.SortExpression
    Catch ex As Exception
        exc = ex
    End Try
    Dim statusEventArgs As New FolderContentsDataSourceStatusEventArgs(exc)
    OnSelected(statusEventArgs)
    If (exc IsNot Nothing And Not statusEventArgs.ExceptionHandled) Then
        Throw exc
    End If
    Return dv
End Function

Points of interest

  • Data source control

Future

Here are some ideas for improvement:

  • Designer support

If anyone decides to extend this control, or has any comments, bug reports, or questions, then it would be great to hear from you.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Web Developer
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFiltering results? Pin
rblehman18-Mar-10 14:08
rblehman18-Mar-10 14:08 
AnswerRe: Filtering results? Pin
Jos Branders19-Mar-10 7:06
Jos Branders19-Mar-10 7:06 
QuestionCan't see relative path? Pin
turbospaz28-Jul-09 7:58
turbospaz28-Jul-09 7:58 
GeneralWould appreciate guide on customising control output Pin
1will26-Jan-09 1:16
1will26-Jan-09 1:16 
GeneralTo Use control without folder permission Pin
dscomazzon18-Nov-08 3:29
dscomazzon18-Nov-08 3:29 
GeneralAJAX Pin
Chris Chang8-Jun-08 11:01
Chris Chang8-Jun-08 11:01 
QuestionSecurity issue ? [modified] Pin
joejoe22228-Jun-07 3:10
joejoe22228-Jun-07 3:10 
AnswerRe: Security issue ? Pin
Jos Branders29-Nov-07 0:59
Jos Branders29-Nov-07 0:59 
AnswerRe: Security issue ? Pin
ly_he19-May-08 21:04
ly_he19-May-08 21:04 
Generalcongratulations it is very nice. Pin
Alaa Jubran15-Jun-07 4:29
Alaa Jubran15-Jun-07 4:29 
GeneralYou Are A God...lol Pin
knahledge15-Jun-07 3:52
knahledge15-Jun-07 3:52 
GeneralRe: You Are A God...lol Pin
Jos Branders16-Jun-07 8:38
Jos Branders16-Jun-07 8:38 
GeneralRe: You Are A God...lol Pin
joejoe22228-Jun-07 10:22
joejoe22228-Jun-07 10:22 
QuestionRe: You Are A God...lol Pin
joejoe2222-Jul-07 2:55
joejoe2222-Jul-07 2:55 
QuestionExtending the control [modified] Pin
knahledge14-Jun-07 9:40
knahledge14-Jun-07 9:40 
AnswerRe: Extending the control Pin
Jos Branders16-Jun-07 8:41
Jos Branders16-Jun-07 8:41 
QuestionProblem with masterpages? Pin
m3ben13-Apr-07 16:29
m3ben13-Apr-07 16:29 
AnswerRe: Problem with masterpages? Pin
Jos Branders14-Apr-07 21:13
Jos Branders14-Apr-07 21:13 
GeneralRe: Problem with masterpages? Pin
m3ben15-Apr-07 15:48
m3ben15-Apr-07 15:48 
GeneralDebuging Errors Pin
egnom19-Mar-07 7:50
egnom19-Mar-07 7:50 
GeneralMaster Pages/UserControl Problem Pin
Al Ortega29-Nov-06 7:39
Al Ortega29-Nov-06 7:39 
GeneralRe: Master Pages/UserControl Problem Pin
Jos Branders2-Dec-06 6:16
Jos Branders2-Dec-06 6:16 
GeneralRe: Master Pages/UserControl Problem Pin
Al Ortega8-Dec-06 15:26
Al Ortega8-Dec-06 15:26 
GeneralNice! [modified] Pin
ManuelaGmail29-Nov-06 4:35
ManuelaGmail29-Nov-06 4:35 
GeneralC# version Pin
fliker29-Nov-06 2:12
fliker29-Nov-06 2:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.