Navigation

Search

Categories

On this page

Displaying all files in a directory and in a datatable

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 112
This Year: 50
This Month: 0
This Week: 0
Comments: 0

Sign In

 Monday, July 09, 2007
Monday, July 09, 2007 8:27:20 AM (Eastern Standard Time, UTC-05:00) ( )

This sample shows how to create a datatable manually, adding files from a directory (using System.IO), and then binding the datatable to a Gridview

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<script language="VB" Runat="server">
Dim dt As DataTable
Dim dr As DataRow
    Sub Page_Load(Source as Object, E as EventArgs)
dt = GetFiles()
gvFiles.DataSource = dt
gvFiles.DataBind()
    End Sub
Public Function GetFiles() As DataTable
Dim strFilePath = Server.MapPath("\test\")
Dim DirInfo As New DirectoryInfo(strFilePath)
Dim Files As FileInfo() = DirInfo.GetFiles()

Dim myTable As New DataTable
myTable.Columns.Add("File Name", Type.GetType("System.String"))
myTable.Columns.Add("Last Write Time", Type.GetType("System.String"))
Dim i As Integer
For i = 0 To Files.Length - 1
Dim Filename As String = Files(i).Name
Dim sWrite As String = Files(i).LastWriteTime
Dim myrow As DataRow
' create new row
myrow = myTable.NewRow
' add files/write times into cells
myrow("File Name") = Filename
myrow("Last Write Time") = sWrite
myTable.Rows.Add(myrow)
Next
Return myTable
End Function    
</script>
<html>
    <head runat="server">        
        <title>Displaying all files in a directory and in a datatable</title>
    </head>
    <body>
<asp:GridView ID="gvFiles" runat="server" Width="432px">
</asp:GridView>
        </form>
    </body>
</html>