VB.Net List Files from Directory

homeshopper
homeshopper used Ask the Experts™
on
I have written a page that lists all files in a specified directory.
Then when a file is selected, it will run that file.
The original program was written in C# and I am trying to convert to VB.
The new page compiles, but when it runs, it only displays the title on the page
'Contents of'. The problem is that it does not display the file names in the directory.
Thanks in advance, full code as follows:
***************************
DefaultFilesVB.aspx.vb
***************************
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Imports System.Drawing
Imports System.Data.OleDb
 
Partial Public Class DefaultFilesVB
    Inherits System.Web.UI.Page
 
    Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        BindFileDataToGrid("Name")
    End Sub
    Public Sub dgFileList_Sort(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
        BindFileDataToGrid(e.SortExpression)
    End Sub
    Public Sub BindFileDataToGrid(ByVal strSortField As String)
        Dim strPath As String = "~/app/"
        Dim myDirInfo As DirectoryInfo = Nothing
        Dim arrFileInfo As Array = Nothing
        Dim myFileInfo As FileInfo = Nothing
        Dim filesTable As New Data.DataTable()
        Dim myDataRow As Data.DataRow = Nothing
        Dim myDataView As Data.DataView = Nothing
        lblPath.Text = strPath
        filesTable.Columns.Add("Name", Type.[GetType]("System.String"))
        filesTable.Columns.Add("Length", Type.[GetType]("System.Int32"))
        filesTable.Columns.Add("LastWriteTime", Type.[GetType]("System.DateTime"))
        filesTable.Columns.Add("Extension", Type.[GetType]("System.String"))
        myDirInfo = New DirectoryInfo(Server.MapPath(strPath))
        arrFileInfo = myDirInfo.GetFiles()
        For Each aFile As Object In arrFileInfo
            Dim thisFile As FileInfo = DirectCast(aFile, FileInfo)
            myDataRow = filesTable.NewRow()
            myDataRow("Name") = thisFile.Name
            myDataRow("Length") = thisFile.Length
            myDataRow("LastWriteTime") = thisFile.LastWriteTime
            myDataRow("Extension") = thisFile.Extension
            filesTable.Rows.Add(myDataRow)
        Next
        myDataView = filesTable.DefaultView
        myDataView.Sort = strSortField
        dgFileList.DataSource = myDataView
        dgFileList.DataBind()
    End Sub
 
    Protected Sub dgFileList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgFileList.SelectedIndexChanged
 
    End Sub
End Class
*******************************
DefaultFilesVB.aspx
********************************
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DefaultFilesVB.aspx.vb" Inherits="DefaultFilesVB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>DefaultFilesVB</title>
</head>
<body>
<form id="form1" runat="server">
    <!--START PAGE SECTION-->
<div>
    <p>
Contents of <strong><asp:Literal id="lblPath" runat="server" /></strong>
</p>
<asp:DataGrid id="dgFileList" runat="server"
    HeaderStyle-BackColor = "#006600"
    HeaderStyle-ForeColor = "#FFFFFF"
    HeaderStyle-Font-Bold = "True"
    ItemStyle-BackColor   = "#CCFFCC"
    AutoGenerateColumns = "False"
	AllowSorting        = "True"
	OnSortCommand       = "dgFileList_Sort">
	<Columns>
		<asp:HyperLinkColumn DataNavigateUrlField="Name" DataNavigateUrlFormatString="app/{0}" DataTextField="Name" HeaderText="File Name:" SortExpression="Name" />
		<asp:BoundColumn DataField="Length" HeaderText="File Size (bytes):" SortExpression="Length" >
            <ItemStyle HorizontalAlign="Right" />
        </asp:BoundColumn>
		<asp:BoundColumn DataField="LastWriteTime" HeaderText="Date Created:" SortExpression="LastWriteTime" />
		<asp:BoundColumn DataField="Extension" HeaderText="File Type:" SortExpression="Extension" />
	</Columns>
</asp:DataGrid>
    </div>
<!--END PAGE SECTION CODE-->
</form> 
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try this :

 
myDirInfo = New DirectoryInfo(Server.MapPath(strPath))
      
        For Each aFile As FileInfo In myDirInfo.GetFiles()
            myDataRow = filesTable.NewRow()
            myDataRow("Name") = aFile.Name
            myDataRow("Length") = aFile.Length
            myDataRow("LastWriteTime") = aFile.LastWriteTime
            myDataRow("Extension") = aFile.Extension
            filesTable.Rows.Add(myDataRow)
        Next

Open in new window

Author

Commented:
I have simplified the program by deleting sorting etc and using a basic datagrid format. I have added your suggested code, but am now not sure what source to bind the data to. I tried the following:
dgFileList.DataSource = myDataRow
dgFileList.DataBind()
am getting error:An invalid data source is being used for dgFileList. A valid data source must implement either IListSource or IEnumerable
I hope the above makes sense. The complete code is as follows:
*************************
DefaultFilesVB.aspx.vb
*************************
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Imports System.Drawing
Imports System.Data.OleDb
Imports ASPEnterpriseManager
Partial Public Class app_DefaultFilesVB
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strPath As String = "~/app/"
        Dim myDirInfo As DirectoryInfo = Nothing
        Dim arrFileInfo As Array = Nothing
        Dim myFileInfo As FileInfo = Nothing
        Dim filesTable As New Data.DataTable()
        Dim myDataRow As Data.DataRow = Nothing
        Dim myDataView As Data.DataView = Nothing
        lblPath.Text = strPath
        filesTable.Columns.Add("Name", Type.[GetType]("System.String"))
        filesTable.Columns.Add("Length", Type.[GetType]("System.Int32"))
        filesTable.Columns.Add("LastWriteTime", Type.[GetType]("System.DateTime"))
        filesTable.Columns.Add("Extension", Type.[GetType]("System.String"))

        myDirInfo = New DirectoryInfo(Server.MapPath(strPath))

        For Each aFile As FileInfo In myDirInfo.GetFiles()
            myDataRow = filesTable.NewRow()
            myDataRow("Name") = aFile.Name
            myDataRow("Length") = aFile.Length
            myDataRow("LastWriteTime") = aFile.LastWriteTime
            myDataRow("Extension") = aFile.Extension
            filesTable.Rows.Add(myDataRow)
        Next
        dgFileList.DataSource = myDataRow
        dgFileList.DataBind()
    End Sub
    Protected Sub dgFileList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgFileList.SelectedIndexChanged

    End Sub
End Class
******************************
DefaultFilesVB.aspx
******************************
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DefaultFilesVB.aspx.vb" Inherits="app_DefaultFilesVB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>DefaultFilesVB</title>
</head>
<body style="background-image: url(../app/images/WebFront.jpg); background-repeat:repeat-x">
<form id="form1" runat="server">
<!--START PAGE SECTION-->
<div>
 <p>
Contents of <strong><asp:Literal id="lblPath" runat="server" /></strong>
</p>
<asp:DataGrid ID="dgFileList" runat="server"
    HeaderStyle-BackColor = "#006600"
    HeaderStyle-ForeColor = "#FFFFFF"
    HeaderStyle-Font-Bold = "True"
    ItemStyle-BackColor   = "#CCFFCC">
<Columns>
<asp:BoundColumn DataField="Length" HeaderText="Name">
</asp:BoundColumn>
</Columns>
    <ItemStyle BackColor="#CCFFCC" />
    <HeaderStyle BackColor="#006600" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
</div>
<!--END PAGE SECTION CODE-->
</form>
</body>
</html>

Author

Commented:
Got it working now, Thank.
dgFileList.DataSource = filesTable
        dgFileList.DataBind()
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Author

Commented:
I am now re-adding sort method:
Public Sub dgFileList_Sort(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
        BindFileDataToGrid(e.SortExpression)
    End Sub
How do I declare or define BindFileDataToGrid

Author

Commented:
now Datagrid as follows:
<asp:DataGrid id="dgFileList" runat="server"
    HeaderStyle-BackColor = "#006600"
    HeaderStyle-ForeColor = "#FFFFFF"
    HeaderStyle-Font-Bold = "True"
    ItemStyle-BackColor   = "#CCFFCC"
      AllowSorting        = "True"
      OnSortCommand       = "dgFileList_Sort">
      <Columns>
            <asp:HyperLinkColumn DataNavigateUrlField="Name" DataNavigateUrlFormatString="app/{0}" DataTextField="Name" HeaderText="File Name:" SortExpression="Name" />
            <asp:BoundColumn DataField="Length" HeaderText="File Size (bytes):" SortExpression="Length" >
            <ItemStyle HorizontalAlign="Right" />
        </asp:BoundColumn>
            <asp:BoundColumn DataField="LastWriteTime" HeaderText="Date Created:" SortExpression="LastWriteTime" />
            <asp:BoundColumn DataField="Extension" HeaderText="File Type:" SortExpression="Extension" />
      </Columns>
    <ItemStyle BackColor="#CCFFCC" />
    <HeaderStyle BackColor="#006600" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>

Author

Commented:
I have re-arranged code to try and get the sort to work.
Any Idea's, Thank in advance. Code as follows:
Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Imports System.Drawing
Imports System.Data.OleDb
Imports ASPEnterpriseManager
Partial Public Class app_DefaultFilesVB
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BindFileDataToGrid("Name")
    End Sub
    Public Sub BindFileDataToGrid(ByVal strSortField As String)
        Dim strPath As String = "~/app/"
        Dim myDirInfo As DirectoryInfo = Nothing
        Dim arrFileInfo As Array = Nothing
        Dim myFileInfo As FileInfo = Nothing
        Dim filesTable As New Data.DataTable()
        Dim myDataRow As Data.DataRow = Nothing
        Dim myDataView As Data.DataView = Nothing
        lblPath.Text = strPath
        filesTable.Columns.Add("Name", Type.[GetType]("System.String"))
        filesTable.Columns.Add("Length", Type.[GetType]("System.Int32"))
        filesTable.Columns.Add("LastWriteTime", Type.[GetType]("System.DateTime"))
        filesTable.Columns.Add("Extension", Type.[GetType]("System.String"))
        myDirInfo = New DirectoryInfo(Server.MapPath(strPath))
        For Each aFile As FileInfo In myDirInfo.GetFiles()
            myDataRow = filesTable.NewRow()
            myDataRow("Name") = aFile.Name
            myDataRow("Length") = aFile.Length
            myDataRow("LastWriteTime") = aFile.LastWriteTime
            myDataRow("Extension") = aFile.Extension
            filesTable.Rows.Add(myDataRow)
        Next
        dgFileList.DataSource = filesTable
        dgFileList.DataBind()
    End Sub
    Public Sub dgFileList_Sort(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
        BindFileDataToGrid(e.SortExpression)
    End Sub
    Protected Sub dgFileList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgFileList.SelectedIndexChanged

    End Sub
End Class

Commented:
I updated following method little bit .

Public Sub BindFileDataToGrid(ByVal strSortField As String)
        Dim strPath As String = "~/app/"
        Dim myDirInfo As DirectoryInfo = Nothing
        Dim arrFileInfo As Array = Nothing
        Dim myFileInfo As FileInfo = Nothing
        Dim filesTable As New Data.DataTable()
        Dim myDataRow As Data.DataRow = Nothing
        Dim myDataView As Data.DataView = Nothing
        lblPath.Text = strPath
        filesTable.Columns.Add("Name", Type.[GetType]("System.String"))
        filesTable.Columns.Add("Length", Type.[GetType]("System.Int32"))
        filesTable.Columns.Add("LastWriteTime", Type.[GetType]("System.DateTime"))
        filesTable.Columns.Add("Extension", Type.[GetType]("System.String"))
        myDirInfo = New DirectoryInfo(Server.MapPath(strPath))
        For Each aFile As FileInfo In myDirInfo.GetFiles()
            myDataRow = filesTable.NewRow()
            myDataRow("Name") = aFile.Name
            myDataRow("Length") = aFile.Length
            myDataRow("LastWriteTime") = aFile.LastWriteTime
            myDataRow("Extension") = aFile.Extension
            filesTable.Rows.Add(myDataRow)
        Next
        If Not filesTable Is Nothing Then
		If strSortField <> "" And filesTable.Columns.Contain(strSortField) Then
		      Dim dv as DataView = new DataView(filesTable)
                      dv.Sort = strSortField
                      filesTable = dv.ToTable() 	
                End 
	End If
        dgFileList.DataSource = filesTable
        dgFileList.DataBind()
    End Sub

Open in new window

Author

Commented:
Thanks, I have one small error still.
If strSortField <> "" And filesTable.Columns.Contain(strSortField) Then
'Contain' is not a member of 'System.DataColumnCollection'
Do I need to add another system reference?
The one I have at the moment are as follows:
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Imports System.Drawing
Imports System.Data.OleDb
Imports ASPEnterpriseManager

Namespace ASPEnterpriseManager.Pages

    Partial Public Class cmsDefaultFilesVB
        Inherits System.Web.UI.Page
Commented:
It should be contains not contain .

If strSortField <> "" And filesTable.Columns.Contains(strSortField) Then

Author

Commented:
Thanks, I have added:
If Not filesTable Is Nothing Then
            If strSortField <> "" And filesTable.Columns.Contains(strSortField) Then
                Dim dv As DataView = New DataView(filesTable)
                dv.Sort = strSortField
                filesTable = dv.ToTable()
            End If
        End If
It now all works. I'll award the points and post complete solution as attached
for others to use. Thanks again.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DefaultFilesVB.aspx.vb" Inherits="cmsDefaultFilesVB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>DefaultFilesVB</title>
</head>
<body style="background-image: url(../app/images/WebFront.jpg); background-repeat:repeat-x">
<form id="form1" runat="server">
<!--START PAGE SECTION-->
<table id="table9" style="z-index:109; position:absolute; top:0px; left:0px; width:auto">
<tr>
<td>
<div style="width:700px; height:625px; font-size:xx-small; overflow:auto;  border: 0px solid #000000">
Heading:9:<%  Response.Write(ConnectionString.Value)%>
Contents of <strong><asp:Literal id="lblPath" runat="server" /></strong>
<asp:DataGrid id="dgFileList" runat="server"
    HeaderStyle-BackColor = "#006600"
    HeaderStyle-ForeColor = "#FFFFFF"
    HeaderStyle-Font-Bold = "True"
    ItemStyle-BackColor   = "#CCFFCC"
	AllowSorting        = "True"
	OnSortCommand       = "dgFileList_Sort">
	<Columns>
		<asp:HyperLinkColumn DataNavigateUrlField="Name" DataNavigateUrlFormatString="app/{0}" DataTextField="Name" HeaderText="File Name:" SortExpression="Name" />
		<asp:BoundColumn DataField="Length" HeaderText="File Size (bytes):" SortExpression="Length" >
            <ItemStyle HorizontalAlign="Right" />
        </asp:BoundColumn>
		<asp:BoundColumn DataField="LastWriteTime" HeaderText="Date Created:" SortExpression="LastWriteTime" />
		<asp:BoundColumn DataField="Extension" HeaderText="File Type:" SortExpression="Extension" />
	</Columns>
    <ItemStyle BackColor="#CCFFCC" />
    <HeaderStyle BackColor="#006600" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
</div>
</td>
</tr>
</table>
<!--END PAGE SECTION CODE-->
</form> 
</body>
</html>
Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Imports System.Drawing
Imports System.Data.OleDb
Imports ASPEnterpriseManager
 
Partial Public Class cmsDefaultFilesVB
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BindFileDataToGrid("Name")
    End Sub
    Public Sub BindFileDataToGrid(ByVal strSortField As String)
        Dim strPath As String = "~/app/"
        Dim myDirInfo As DirectoryInfo = Nothing
        Dim arrFileInfo As Array = Nothing
        Dim myFileInfo As FileInfo = Nothing
        Dim filesTable As New Data.DataTable()
        Dim myDataRow As Data.DataRow = Nothing
        Dim myDataView As Data.DataView = Nothing
        lblPath.Text = strPath
        filesTable.Columns.Add("Name", Type.[GetType]("System.String"))
        filesTable.Columns.Add("Length", Type.[GetType]("System.Int32"))
        filesTable.Columns.Add("LastWriteTime", Type.[GetType]("System.DateTime"))
        filesTable.Columns.Add("Extension", Type.[GetType]("System.String"))
        myDirInfo = New DirectoryInfo(Server.MapPath(strPath))
        For Each aFile As FileInfo In myDirInfo.GetFiles()
            myDataRow = filesTable.NewRow()
            myDataRow("Name") = aFile.Name
            myDataRow("Length") = aFile.Length
            myDataRow("LastWriteTime") = aFile.LastWriteTime
            myDataRow("Extension") = aFile.Extension
            filesTable.Rows.Add(myDataRow)
        Next
        If Not filesTable Is Nothing Then
            If strSortField <> "" And filesTable.Columns.Contains(strSortField) Then
                Dim dv As DataView = New DataView(filesTable)
                dv.Sort = strSortField
                filesTable = dv.ToTable()
            End If
        End If
 
        dgFileList.DataSource = filesTable
        dgFileList.DataBind()
 
    End Sub
    Public Sub dgFileList_Sort(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
        BindFileDataToGrid(e.SortExpression)
    End Sub
    Protected Sub dgFileList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgFileList.SelectedIndexChanged
 
    End Sub
End Class

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial