The .NET Framework
comes fully equipped with a set of classes to carry out basic file system tasks—creating
and reading directories, checking to see if files exist, retrieving file
properties, and so on. Most of this magic takes place through two built-in objects,
the FileInfo() object and the DirectoryInfo() object, both part of the
System.IO namespace.

In this article, I
will introduce you to these two objects. I’ll show you how to read directories,
retrieve file properties, and create a Windows Explorer clone that dynamically
generates directory listings and file information.

Checking if a directory exists

My first example,
Listing A, is a simple ASP.NET script that checks for the
existence of a particular directory. This is a critical step when developing
applications that interact with the file system, and one that many newbie
programmers forget.

As you will see when
you try running the script above, the code tells you whether the named
directory exists or not. It’s able to do this because of the Exists property of the DirectoryInfo()
class, which I imported as part of the System.IO namespace at the beginning of
the script.

The class constructor
requires an input argument—the name of the directory to be tested. The Exists property then stores a Boolean value—true
if the named directory exists and false if it doesn’t.

Important: The special
“ASPNET” user must have the privileges necessary to view the named
directory in order for the script to work correctly.

Dude, where’s my file?

Now that you’ve been
introduced to the DirectoryInfo() class, let’s turn our attention to files and
the FileInfo() object. Take a look at the script in Listing B. This expands on the functionality in Listing A—it tests
for the presence of a file, and then prints more information about it if it
exists.

If the named file
exists, here is what the output of the script above might look like:

The file E:/users/john/personal/files/Test_Document.doc
was found. Here is more information:
Name: Test_Document.doc
Location: E:/users/john/personal/files/Test_Document.doc
Created on: 08/07/2004 12:54:00
Last modified on: 08/07/2004 12:54:00
File size (in bytes): 10752
Extension: .doc

Obviously, if the file
cannot be found, you’ll see an error message telling you all about it.

As before, I’ve begun
by creating an instance of the FileInfo() object, and passing the name and
location of the file to the constructor. Next, I use the Exists property of the FileInfo() object to check if the file
exists. If it does, I use a bunch of other properties of the FileInfo() object
to obtain more information about the file:

  • the Name property, which stores the name of
    the file
  • the FullName property, which stores the full
    path to the file (including the drive letter)
  • the CreationTime and LastWriteTime properties, which represent the times the file was
    originally created and last modified
  • the Length property, which stores the size
    of the file in bytes (note my use of the ToString() method to convert the
    number into a string)
  • the Extension property, which
    stores the file extension

There are more
properties too—take a look at Microsoft’s documentation for more information. And incidentally,
you can use these same properties for the DirectoryInfo() class as well. Try it
for yourself (after reading up on the class) and see.

Digging deeper

So far, you’ve seen
the important properties of the DirectoryInfo() and FileInfo() objects. But
where there’s a property, there’s a method not far behind. Listing C illustrates some of the methods associated with these
objects, by iterating over a given folder and displaying the names of subfolders
and files in it.

While the code listing
may look complicated, it’s pretty straightforward. The script uses the
GetDirectories() and GetFiles() methods of the DirectoryInfo() object to
retrieve the contents of a given folder. These two methods return arrays of
DirectoryInfo() and FileInfo() objects respectively. These returned arrays are
stored in the aryDirectories[] and aryFiles[] arrays. The foreach() loop
iterates over the objects inside these arrays, and prints the names of the
files and directories inside them via their respective Name properties.

Intrepid exploring

Using what we’ve learned,
it’s easy to construct a simple file viewer that lists the contents of folders
and subfolders dynamically a la the
Windows Explorer. Take a look at the code in
Listing D.

Here is what you might
see when you run the page:

Figure A

A Windows Explorer-like view of the
directory using .NET

To understand the
code, begin with the two string variables, strDriveLetter and strDefaultPath. The
first stores the drive letter, and the second stores the relative path to the
starting folder. For the above example, these variables are set to
“E:/” and “users/john/personal/files”. Note also the
strPath variable, which always contains the path to the current folder. This
variable is passed as a GET parameter in the URL, and is constantly modified by
the script as it moves from one folder to another. Since this variable contains
no value when the script is first loaded, it is assigned the default value
stored in strDefaultPath.

Next up is the custom DisplayDirContents()
function. This function concatenates the strPath variable passed as an input
argument with the drive letter to create an instance of the DirectoryInfo()
object. Then, it retrieves the list of directories and files using the object’s
GetDirectories() and GetFiles() methods. For files, the important properties
are printed.

Note that a hyperlink
is created for each folder so the user can navigate further with the name of
the current folder appended to the strPath variable.

There’s a lot more to these
two objects as well—I encourage you to examine the APIs closely, and come up
with creative uses for the numerous built-in methods. I hope that this article helps
get you started.