dir_to_rst.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
Dim strPath As String = "./dir/"
Dim myDirInfo As DirectoryInfo
Dim arrFileInfo As Array
Dim myFileInfo As FileInfo
Dim filesTable As New DataTable
Dim myDataRow As DataRow
Dim myDataView As DataView
' The table is pretty simple... could store more info, but this is
' all I want to display so it's all I'm storing.
filesTable.Columns.Add("File Name:", Type.GetType("System.String"))
filesTable.Columns.Add("File Size (bytes):", Type.GetType("System.Int32"))
filesTable.Columns.Add("Date Created:", Type.GetType("System.DateTime"))
' Get Directory Info
myDirInfo = New DirectoryInfo(Server.MapPath(strPath))
' Get File Info
arrFileInfo = myDirInfo.GetFiles()
' Loop through array of FileInfo objects placing the data we'll
' be using into our DataTable for easy manipulation.
For Each myFileInfo In arrFileInfo
myDataRow = filesTable.NewRow()
myDataRow("File Name:") = myFileInfo.Name
myDataRow("File Size (bytes):") = myFileInfo.Length
myDataRow("Date Created:") = myFileInfo.CreationTime
filesTable.Rows.Add(myDataRow)
Next myFileInfo
' Create a new DataView and Filter to show files under
' 500 bytes for illustration. Notice that I used spaces
' in the field name so they'd display nicely, that means I
' need to use []s so that I don't confuse things when I filter.
myDataView = filesTable.DefaultView
myDataView.RowFilter = "[File Size (bytes):] < 500"
' Set DataGrid's data source and data bind.
dgFileList.DataSource = myDataView
dgFileList.DataBind()
End Sub
</script>
<html>
<head>
<title>ASP.NET Directory List To DataTable Sample</title>
</head>
<body>
<form runat="server">
<p>
Only showing files that are smaller then 500 bytes:
</p>
<asp:DataGrid id="dgFileList" runat="server"
Border = 3
BorderColor = "black"
CellSpacing = 0
CellPadding = 2
HeaderStyle-BackColor = "#CCCC99"
HeaderStyle-ForeColor = "#000000"
HeaderStyle-Font-Bold = "True"
ItemStyle-BackColor = "#FFFFCC"
/>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/dir_to_rst_aspx.asp">here</a> to read about and download the source code.
</p>
</body>
</html>