Discover Directoy and File Data...
By: John Kilgo Date: January 2, 2003 Download the code.


This is a short article, and a small amount of code, to get you started in discovering data about the files contained in a specified directory. In the article you will be exposed to the DirectoryInfo and FileInfo members of System.IO. Actually, you have a couple of choices when discovering directory information. The Directory class contains shared methods for working with directories while the DirectoryInfo class contains instance methods. For purposes of this article I chose to deal with the DirectoryInfo class.

We also have a choice when dealing with file information within a directory. Similar to the above, the File class contains static methods while the FileInfo class contains non-static methods. One of the advantages of the FileInfo class is that when you create an instance of it, the system performs security checks the first time you use the file. The File class on the otherhand, performs security checks everytime you call one of its methods. The FileInfo class is therefore more efficient, especially if you are going to perform several operations on a file. For purposes of this artice I chose to deal with the FileInfo class.

The first piece of code is a very short .aspx file to create an instance of the DataGrid control. The main processing is reserved for an equally short code behind page.

<%@ Page Inherits="GetDirectoryInfo" Src="GetDirectoryInfo.aspx.vb" %>

<html>
<body>
<form runat="server" ID="Form1">
<asp:label id="lblMessage" runat="server" />
<p>
<asp:DataGrid runat="server" id="dtgDirInfo">
</asp:DataGrid>
</p>
</form>
</body>
</html>
Now for the code behind file where the work is done. The first order is to specify which directory we want to inspect for files. As you can see I chose to look at the "runtime" subdirectory of my virtual web. You can just as easily specify a (complete) hard-coded directory path if you wish. Next we create an instance of type DirectoryInfo named dirInfo specifying the path to the directory we want to look in. We then dimension an array variable (arrFiles) which fills itself by having FileInfo call the GetFiles method of our DirectoryInfo instance. We then bind the array to our DataGrid and we are done. Once you run this program you will see the array of information that FileInfo and DirectoryInfo can provide about the files contained in the target directory.
Public Class GetDirectoryInfo : Inherits System.Web.UI.Page

  Protected dtgDirInfo As System.Web.UI.WebControls.DataGrid
  Protected lblMessage As System.Web.UI.WebControls.Label

  Sub Page_Load(ByVal Source As Object, ByVal E As System.EventArgs)
    Dim strDir As String

    strDir = Server.MapPath("~/runtime")
    lblMessage.Text = "<b>Directory Listing of " & strDir & ":</b>"

    Dim dirInfo as New System.IO.DirectoryInfo(strDir)
    Dim arrFiles as System.IO.FileInfo() = dirInfo.GetFiles()

    dtgDirInfo.DataSource = arrFiles
    dtgDirInfo.DataBind()
  End Sub
End Class
I realize this artice is short, but hopefully you can see from the code that there really isn't much work to be done to obtain useful information on the files contained in a directory.

You may run this program by clicking Here. You may download the code by clicking Here.