The ASPSmith Articles, ASPAlliance.com

Retrieving File Information In ASP.NET

By Steven Smith
http://www.aspalliance.com/stevesmith/articles/dotnetfileinfo.asp
[Example]

Using ASP.NET it is very easy to extract information about a file, including its name, path, extension, size, and various access and creation datetimes. This article quickly goes over the code needed to gather information about files.

As with many things in ASP.NET, the .NET Framework is going to do all of our work for us. Using Classic ASP, it was necessary to use the Scripting.FileSystemObject to reference information about files. In .NET, this is replaced with the System.IO namespace, which includes a number of classes for referencing file system information. One class in particular that exposes pretty much all of the information you'd ever want to know about a file is aptly named FileInfo. The example, detailed below, uses this object to extract quite a bit of information about a file (in this case, itself). Note that you can also use the FileInfo class to perform operations on files, such as copying or renaming, but that is the subject of another article.

fileinfo.aspx (C#)
<%@ Page Language="c#" %>
<script runat="server">

protected System.IO.FileInfo objFI;
protected String filename;

protected void Page_Load(){
    if(!IsPostBack){
        // Get the path of this file
        filename = Request.ServerVariables["PATH_TRANSLATED"];

        // Create a new FileInfo object for this filename
        objFI = new System.IO.FileInfo(filename);

        // Populate File Information Fields
        fullname.Text = objFI.FullName;
        name.Text = objFI.Name;
        size.Text = objFI.Length.ToString();
        created.Text = objFI.CreationTime.ToString();
        accessed.Text = objFI.LastAccessTime.ToString();
        modified.Text = objFI.LastWriteTime.ToString();
        directory.Text = objFI.DirectoryName;
        extension.Text = objFI.Extension;
    }
}
</script>
<HTML>
<HEAD>
<link type="text/css" rel="stylesheet" href="http://aspalliance.com/stevesmith/include/ss.css" /></head>
</HEAD>
<BODY>
<form runat="server">
<b>File Information</b>
<table class="articlelist">
<tr>
    <td class="header">Full Name</td>
    <td>
        <asp:Label id="fullname" runat="server"/>
    </td>
</tr>
<tr>
    <td class="header">Name</td>
    <td>
        <asp:Label id="name" runat="server"/>
    </td>
</tr>
<tr>
    <td class="header">Extension</td>
    <td>
        <asp:Label id="extension" runat="server"/>
    </td>
</tr>
<tr>
    <td class="header">Size</td>
    <td>
        <asp:Label id="size" runat="server"/>
    </td>
</tr>
<tr>
    <td class="header">Created</td>
    <td>
        <asp:Label id="created" runat="server"/>
    </td>
</tr>
<tr>
    <td class="header">Modified</td>
    <td>
        <asp:Label id="modified" runat="server"/>
    </td>
</tr>
<tr>
    <td class="header">Accessed</td>
    <td>
        <asp:Label id="accessed" runat="server"/>
    </td>
</tr>
<tr>
    <td class="header">Parent Folder</td>
    <td>
        <asp:Label id="directory" runat="server"/>
    </td>
</tr>
</table>
</form>
</BODY>
</HTML>
Summary

The code should be pretty self-explanatory. First I set a filename variable to the path of the current file (itself), then I instantiate the FileInfo class as the variable objFI, passing the filename into the constructor so that my instance of the object knows which file I'm going to refer to. Having done that, all of the FileInfo class's public properties are available to me, including:

  • CreationTime
  • DirectoryName
  • Extension
  • FullName
  • LastAccessTime
  • LastWriteTime
  • Length (in bytes)
  • Name
At this point I just set the Text property of my individual label fields to the values of these properties, and that's all, folks! Hope this helps; amazingly I couldn't find any other articles that cover this important topic. When I do, I'll post some related links.