Click here to Skip to main content
12,067,646 members (34,920 online)
Click here to Skip to main content
Add your own
alternative version


60 bookmarked

File System Browsing in ASP.NET: New Approach vs. Old Approach

, 3 Jul 2005
Rate this:
Please Sign up or sign in to vote.
Most of the programmers who have transitioned from ASP to ASP.NET still use the loop-and-create method for displaying contents of a specific folder. In this demo I am going to show you how to use the File System Objects, a DataGrid and a DataTable to accomplish this task.


"File system browsing? What the heck is that?"

"Old way? New way? is there another way???"

I guess most of us are still on the pre-.NET mindset, the term I commonly use when people think of solutions that are based on the ways of doing stuff without utilizing the benefits of the new technology which could help in making the task a lot easier. A great example of this type of mindset is this, what comes to your mind when you are asked to create a Web Form that enables the user to list the contents of a folder and enables the user to download selected files on that folder? I bet you more than 80% of the programmers would have this answer.

"Yeah, I can build a function that reads the folder and write it like what I used to do on ASP 3.0 where I build the layout using HTML tags and just loop thru all the directories and files inside the specified folder. I would then use the Response.Write method to write the structure of the table to an HTML page."


I guess most of us still think this way...

What if I tell you that you can do this task by just using some System.IO objects, a DataTable and a DataGrid? Strange huh? But it's true! Before we proceed to the solution, I want to show you an example of a code written by an ASP.NET programmer that uses the old way of accomplishing this task:

Here is the C# example of the classic code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Configuration;
namespace KeithRull.FolderBrowserCS
    /// <span class="code-SummaryComment"><summary>

Doesn't this look like an old ASP implementation? Working with the interface while reading the data?

As I said a while ago, we can accomplish the same task by using DataTables and a DataGrid. First, let's do a little examination of the DataGrid.DataSource property. DataGrid.DataSource property accepts any object that implements an IList interface. With this in mind, let's take a look at the values returned by DirectoryInfo.GetDirectories(), DirectoryInfo.GetFiles() and Directory.FileSystemInfo().>.

The DirectoryInfo.GetDirectories() is a method that is used when you want to get a collection of all the subdirectories inside a directory. While the DirectoryInfo.GetFiles() gets a collection of all the files inside a specified directory. On the other hand, DirectoryInfo.GetFileSystemInfos() gets a collection of all the files and directories inside a folder. Since these three methods return a collection of files, directories or both, we can safely assume that we can use the result set returned by these objects as the data source of our DataGrid.

The listing below shows how to list the contents of a folder using DirectoryInfo.FileSystemInfo():

private void Page_Load(object sender, System.EventArgs e)
    string folderToBrowse = @"c:\";
    DirectoryInfo DirInfo = 
        new DirectoryInfo(folderToBrowse);
    FileSystemGrid.DataSource = 

Please take a note that you need to setup your grid to accept the values that are generated by the DirectoryInfo.GetFileSystemInfo method. A sample template is as follows:

<asp:datagrid id="FileSystemGrid" runat="server" 
    BorderStyle="None" AutoGenerateColumns="False"
    Font-Size="XX-Small" Font-Names="Arial" 
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="350px"></HeaderStyle>
<asp:HyperLink id=systemLink runat="server" NavigateUrl='' 
   Text='<%# DataBinder.Eval(Container, "DataItem.FullName") %>'>
<asp:BoundColumn DataField="CreationTime" 
<HeaderStyle Width="150px"></HeaderStyle>
<asp:BoundColumn DataField="LastWriteTime" 
<HeaderStyle Width="150px"></HeaderStyle>

The result of our first sample is shown below:

This is simple? right?

What if we want to add some user interactions? For example, clicking on a selected directory would allow the user to browse the underlying files and directories. To accomplish this task, we need to have a LinkButton, our DataTable and our DataGrid. But this time, we would be separating the Folder table and the File table. This is necessary because we want to have different interactions. For example, clicking a folder would enable us to browse the sub folder and files on that folder or clicking on the file would enable us to download that file.

Here is the C# example of the solution to this problem:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace KeithRull.FolderBrowserCS
    /// <span class="code-SummaryComment"><summary>

The screenshot of the result of our solution is shown below:

As you can see, we have accomplished the same result as the first example that uses the legacy ASP logic using native ASP.NET objects.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Keith Rull
Web Developer
United States United States
Keith Rull is a programmer from the Philippines who recently moved to San Diego, CA.

He has vast experience in developing applications using C#, Vb.NET, Java, Oracle, Microsoft Reporting Services and Microsoft SQL Server.

When not infront of the computer, he usually spends his time thinking about his motherland.

You may also be interested in...

Comments and Discussions

Questionthis is what I need Pin
saichander12331-Jul-12 18:57
membersaichander12331-Jul-12 18:57 
GeneralMy vote of 5 Pin
Mamun Reza18-Oct-11 0:45
memberMamun Reza18-Oct-11 0:45 
GeneralMy vote of 5 Pin
Jamie Llewellyn6-Oct-11 17:55
memberJamie Llewellyn6-Oct-11 17:55 
GeneralExcellent code Pin
Dan B. Nielsen17-Nov-09 3:12
memberDan B. Nielsen17-Nov-09 3:12 
QuestionHow to show a icon associated to file Pin
falejandrocom25-Jan-09 20:42
memberfalejandrocom25-Jan-09 20:42 
AnswerRe: How to show a icon associated to file Pin
Keith Oliver Rull26-Jan-09 5:39
memberKeith Oliver Rull26-Jan-09 5:39 
Questionhow to make it all download at time using button click on page Pin
harish44823-May-08 22:11
memberharish44823-May-08 22:11 
GeneralNew version! Pin
Keith Oliver Rull19-Nov-07 7:08
memberKeith Oliver Rull19-Nov-07 7:08 
GeneralSorting Pin
Adam953614-Sep-07 6:03
memberAdam953614-Sep-07 6:03 
GeneralGenius I say Pin
knahledge15-Jun-07 6:43
memberknahledge15-Jun-07 6:43 
QuestionHow can we make the datagrid sortable? Pin
nancydo30-Apr-07 5:52
membernancydo30-Apr-07 5:52 
GeneralError while running locally Pin
xccx19-Mar-07 21:39
memberxccx19-Mar-07 21:39 
GeneralHi Pin
taminha21-Feb-07 22:31
membertaminha21-Feb-07 22:31 
GeneralRe: Hi Pin
Keith Oliver Rull23-Feb-07 12:03
memberKeith Oliver Rull23-Feb-07 12:03 
QuestionIs there a ASP classic version of this? Pin
Member #37897762-Feb-07 9:06
memberMember #37897762-Feb-07 9:06 
GeneralFile download incomplete Pin
AndyBerga26-Jan-07 2:06
memberAndyBerga26-Jan-07 2:06 
GeneralRe: File download incomplete Pin
Keith Oliver Rull2-Feb-07 9:48
memberKeith Oliver Rull2-Feb-07 9:48 
GeneralI get a compilation error Pin
cygnusx19730-Nov-06 11:39
membercygnusx19730-Nov-06 11:39 
GeneralRe: I get a compilation error Pin
Forogar8-Dec-06 7:18
memberForogar8-Dec-06 7:18 
GeneralThanks for the article [modified] Pin
Boro_Bob25-Aug-06 5:47
memberBoro_Bob25-Aug-06 5:47 
QuestionCan you sort fileSystemCombinedTable by Name, Length, and LastWriteTime on ASC and DESC? Pin
Mary L28-Feb-06 9:43
memberMary L28-Feb-06 9:43 
AnswerRe: Can you sort fileSystemCombinedTable by Name, Length, and LastWriteTime on ASC and DESC? Pin
Keith Oliver Rull28-Feb-06 9:47
memberKeith Oliver Rull28-Feb-06 9:47 
GeneralRe: Can you sort fileSystemCombinedTable by Name, Length, and LastWriteTime on ASC and DESC? Pin
yangmingchao20-Jun-07 17:21
memberyangmingchao20-Jun-07 17:21 
QuestionA better alternative? Pin
Kerem Kat27-Jan-06 13:02
memberKerem Kat27-Jan-06 13:02 
Generalnice Pin
Taha Zayed7-Jul-05 3:47
memberTaha Zayed7-Jul-05 3:47 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160208.1 | Last Updated 3 Jul 2005
Article Copyright 2005 by Keith Rull
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid