Use System.IO to Explore Data About Your Files...
By: John Kilgo Date: December 1, 2002 Download the code.


This is a fairly simple little program to get you started exploring the System.IO namespace. In this program we will discover the Attributes, Creation Date and Time, Last Accessed date and time, and Last Modified date and time of a file on disk. This example happens to use a simple text file, but the same methods work on any type of file.

The first code shown is ShowFileInfo.aspx which contains some labels and text boxes to hold the pertinent data. The real work is done in the code-behind file shown further below.

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ShowFileInfo</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
  <asp:Label id="Label1"
    style="Z-INDEX: 101; LEFT: 25px; POSITION: absolute; TOP: 68px"
    runat="server" Width="68px" Height="26px"
    text="File Name:">
  </asp:Label>
  <asp:Label id="lblFileName"
    style="Z-INDEX: 102; LEFT: 105px; POSITION: absolute; TOP: 64px"
    runat="server" Width="96px" Height="26px"
    BackColor="LightGrey" BorderColor="Navy" BorderStyle="Solid"
    text="SomeFile.txt">
  </asp:Label>
  <asp:Button id="btnShowData"
    style="Z-INDEX: 103; LEFT: 25px; POSITION: absolute; TOP: 106px"
    runat="server" Width="151px" Text="Show File Information">
  </asp:Button>
  <asp:TextBox id="txtAttributes" runat="server"
    style="Z-INDEX: 103; LEFT: 25px; POSITION: absolute; TOP: 150px"
    width="220px">
  </asp:TextBox>
  <asp:TextBox id="txtCreated" runat="server"
    style="Z-INDEX: 103; LEFT: 25px; POSITION: absolute; TOP: 172px"
    width="220px">
  </asp:TextBox>
  <asp:TextBox id="txtAccessed" runat="server"
    style="Z-INDEX: 103; LEFT: 25px; POSITION: absolute; TOP: 194px"
    width="220px">
  </asp:TextBox>
  <asp:TextBox id="txtModified" runat="server"
    style="Z-INDEX: 103; LEFT: 25px; POSITION: absolute; TOP: 216px"
    width="220px">
  </asp:TextBox>
</form>
</body>
</HTML>
The code-behind file (ShowFileInfo.aspx.vb) contains the code to gather the information we want on a file name "SomeFile.txt". Notice that the System.IO Namespace has been imported to make its methods available without having to type in the fully qualified method names.

The important code is near the bottom of this file. We are setting TextBox text properties equal to the return values from methods such as File.GetAttributes, File.GetCreationTime, etc. This file is quite simple, but perhaps it will get you started investigating the many methods available to you in the System.IO Namespace. Good hunting!

Imports System
Imports System.IO
Imports System.Text
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class ShowFileInfo : Inherits Page

  Protected WithEvents Label1 As Label
  Protected WithEvents lblFileName As Label
  Protected WithEvents btnShowData As Button
  Protected WithEvents txtFileData As TextBox
  Protected WithEvents txtAttributes As TextBox
  Protected WithEvents txtCreated As TextBox
  Protected WithEvents txtAccessed As TextBox
  Protected WithEvents txtModified As TextBox

  Private strFileName As String

  Private Sub btnShowData_Click( _
    ByVal Sender As Object, ByVal e As EventArgs) Handles btnShowData.Click
    Dim strBuilder As New StringBuilder(800)

    strFileName = lblFileName.Text
    strFileName = Server.MapPath(strFileName)
    txtAttributes.Text = "Attributes: " & File.GetAttributes(strFileName).ToString()
    txtCreated.Text = "Created: " & File.GetCreationTime(strFileName).ToString()
    txtAccessed.Text = "Accessed: " & File.GetLastAccessTime(strFileName).ToString()
    txtModified.Text = "Modified: " & File.GetLastWriteTime(strFileName).ToString()
  End Sub

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