We also have a choice when dealing with file information within a directory. Similar to the above, the File class contains static methods while the FileInfo class contains non-static methods. One of the advantages of the FileInfo class is that when you create an instance of it, the system performs security checks the first time you use the file. The File class on the otherhand, performs security checks everytime you call one of its methods. The FileInfo class is therefore more efficient, especially if you are going to perform several operations on a file. For purposes of this artice I chose to deal with the FileInfo class.
The first piece of code is a very short .aspx file to create an instance of the DataGrid control. The main processing is reserved for an equally short code behind page.
|
<%@ Page Inherits="GetDirectoryInfo" Src="GetDirectoryInfo.aspx.vb" %> <html> <body> <form runat="server" ID="Form1"> <asp:label id="lblMessage" runat="server" /> <p> <asp:DataGrid runat="server" id="dtgDirInfo"> </asp:DataGrid> </p> </form> </body> </html> |
|
Public Class GetDirectoryInfo : Inherits System.Web.UI.Page Protected dtgDirInfo As System.Web.UI.WebControls.DataGrid Protected lblMessage As System.Web.UI.WebControls.Label Sub Page_Load(ByVal Source As Object, ByVal E As System.EventArgs) Dim strDir As String strDir = Server.MapPath("~/runtime") lblMessage.Text = "<b>Directory Listing of " & strDir & ":</b>" Dim dirInfo as New System.IO.DirectoryInfo(strDir) Dim arrFiles as System.IO.FileInfo() = dirInfo.GetFiles() dtgDirInfo.DataSource = arrFiles dtgDirInfo.DataBind() End Sub End Class |
You may run this program by clicking Here. You may download the code by clicking Here.