Displaying the Files in a Directory using a DataGrid

This article was written in 2003 and shows how to display files in the DataGrid Web control. Since ASP.NET 2.0, the DataGrid has been
deprecated in favor of the GridView control. A new article, Displaying Files and Folders in a GridView,
examines how to display files and folders in a GridView control.

Introduction

There are many scenarios Web developers face where they need to be able to display information about
the Web server's file system. For example, a Web hosting company might want to provide a
"Control Panel" that allows their clients to edit their Web site's files using a Web interface.
A developer working on her company's intranet might want to create a Web page that provides
a nice listing of business reports that reside in an intranet-accessible directory.

In classic ASP, to access the Web server's file system developers used the FileSystemObject library.
(For more on the FileSystemObject library check out the FileSystemObject
FAQ Category at ASPFAQs.com.) In .NET, there is a set of classes in the System.IO namespace
that allow for programmatic access to the file system.

Accessing file system information in ASP.NET is about as easy (if not easier) as it was in classic
ASP. The real benefit with ASP.NET comes in displaying the file system information. As we will see
in this article, the file system information can be bound to any of the data Web controls, such as
the DataGrid, DataList, or Repeater. This means that the information can be displaying in an aesthetically-pleasing
manner with minimal time.

Examining Accessing the File System Using ASP.NET

The .NET Framework contains two classes for accessing directory information, and two classes for
accessing file information. To access directory information, use either the Directory
or the DirectoryInfo class; to access file information, use either the File
class or the FileInfo class.

The differences between the two classes is the level of information it returns and how it is used.
The Directory and File classes are static classes, meaning that you
don't have to create an instance of the class in order to use its methods. These classes are useful
if you want to quickly perform some directory-related function. For example, to delete a file, you can
use File.Delete(filePath); to determine if a directory exists, you can use
Directory.Exists(directoryPath).

The Info classes -
FileInfo and DirectoryInfo - require that you first create an instance
of the class and specify the file or directory's name in the constructor. For example, to delete a
file using the FileInfo class, you'd use the following code:

Dim myFile as FileInfo = New FileInfo(filePath)
myFile.Delete()

Getting a List of Files in a Directory

Both the Directory and DirectoryInfo classes contain a method to get all
of the files in a directory (or to get all of the files in a directory matching some wildcard expression,
like *.htm) - this method is called GetFiles() and is used as follows:

As you can see, the Directory.GetFiles() method accepts one or two parameters. You
must specify the path of the directory whose files you want to get, and you may optionally
specify a wildcard path (like *.aspx). This method returns a String array, which contains
the filenames in the directory (that match the wildcard expression, if supplied). The
DirectoryInfo.GetFiles() method doesn't accept a directory path input since the directory's
path is already known. Unlike the Directory.GetFiles() method, the
DirectoryInfo.GetFiles() method returns an array of FileInfo objects, not
Strings.

Displaying a Directory's Files in a DataGrid

To display a directory's files in a DataGrid (or DataList or Repeater) all we need to do is assign
the String array or FileInfo array to the DataGrid's DataSource property
and then call the DataGrid's DataBind() method. For this example, we'll use the
DirectoryInfo.GetFiles() method instead of the Directory.GetFiles() method.
If we opted to use the Directory.GetFiles() method then all we'd be able to show in
the DataGrid is the file's name. By using the DirectoryInfo.GetFiles() method instead,
we can display other attributes of the file, such as its size, last modified date, and so on.

As the above code sample shows, all that it takes to bind the list of files in a directory to
an ASP.NET DataGrid is simply setting the DataGrid's DataSource to the result of the
GetFiles() method. Specifically, in the above code sample the list of files with the
extension .aspx that reside in the same directory that the ASP.NET Web page itself
resides in are displayed.

Particular attributes of the FileInfo class can be displayed in the various DataGrid
columns by simply setting the column's properties accordingly. For example, the FileInfo.Length
property is displayed in the last BoundColumn by setting the BoundColumn's DataField
property to Length. (For a full list of FileInfo properties, see the
technical
documentation.)

Now that we've seen how to simply display the list of files in a directory, let's look at making
the example a bit more useful by allowing users to delete a file with the click of a button.
We'll see how to accomplish this in Part 2.