The ASPSmith Articles, ASPAlliance.com

ASP.NET Directory Lister

By Steven Smith
http://www.aspalliance.com/stevesmith/articles/directorylist.asp

A common requirement for a website is to provide access to a group of files in a particular directory. Assuming directory browsing is not enabled, it is necessary to write code to do this. The following files demonstrate a simple way to do this using ASP.NET, and includes the ability to filter files so that only certain types are shown. The first file, default.aspx, lists filename, last modified date/time, and size in bytes of the files (and defines the layout of the page). The second file is the codebehind for default.aspx, and performs the actual programming logic to do all of this.

default.aspx
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="download.Cdefault" %>
<html>
<body>
<h1>
    ASP.NET By Example: Downloads
</h1>
<div align="right">
<a href="../">Table of Contents</a>
</div>
<br/>
<asp:datalist id="filegrid" runat="server" repeatdirection="Vertical" repeatcolumns="1">
    <HeaderTemplate>
        <table cellpadding="5" cellspacing="0" border="1">
            <tr bgcolor="#CCCCFF">
                <th>
                    Filename</th>
                <th>
                    Last Modified</th>
                <th>
                    Size</th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <a href="<%# DataBinder.Eval(Container.DataItem, ""Name")%>">
                    <%# DataBinder.Eval(Container.DataItem, "Name")%>
                </a>
            </td>
            <td>
                <%# DataBinder.Eval(Container.DataItem, "LastWriteTime")%>
            </td>
            <td>
                <%-- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconstandardnumericformatstrings.asp --%>
                <%# ((System.IO.FileInfo)Container.DataItem).Length.ToString("n0")%>
                bytes
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:datalist>
</body>
</html>
csharpindex.com/colorCode

default.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace download
{
    /// <summary>
    /// Summary description for Cdefault.
    /// </summary>
    public class Cdefault : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataList filegrid;

        public Cdefault()
        {
            Page.Init += new System.EventHandler(Page_Init);
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            //display current directory
            ArrayList fileArray = new ArrayList();
            FileInfo objFI;
            string[] files;
            files = System.IO.Directory.GetFiles(Server.MapPath("/aspnetbyexample/download/"),"*.zip");
            for(int i= 0;i< files.Length;i++)
            {
                // Create a new FileInfo object for this filename
                objFI = new System.IO.FileInfo(files[i]);
                fileArray.Add(objFI);
            }
            filegrid.DataSource = fileArray;
            filegrid.DataBind();
        }

        private void Page_Init(object sender, EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
        }

        #region Web Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion

    }
}
csharpindex.com/colorCode

This particular file is used to list all files in a folder that end in ".zip". You can see an example of this utility at work in the downloads section of my book's website at http://ASPAuthors.com/ASPNETByExample/. Not too complicated, but it should save you a few minutes' coding, and it's easily modified. It might make a good user control or custom control, too, but I never got that far with it.