This tutorial will show you how to display file properties , such as view create date, last access date, last modify date and file size, using ASP.NET 2.0 and VB.NET.
This tutorial will show you how to display file properties , such as view create date, last access date, last modify date and file size, using ASP.NET 2.0 and VB.NET.
First, you will need to import the System.IO namespace.The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support. Contains the class of FileInfo ,DirectoryInfo and Path.
Use the DirectoryInfo class for typical operations such as copying, moving, renaming, creating, and deleting directories.
Use the FileInfo class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files.
Use the Path class for performs operations on String instances that contain file or directory path information.
| Imports System.IO |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
We use the buttonDisplay_Click event , the buttonUp_Click event, the listBoxFolders_SelectedIndexChanged event ,and the listBoxFiles_SelectedIndexChanged event to do the work. Then we call DisplayFileInfo() and DisplayFolderList() to enumerating through directories, subdirectories and files.
ButtonDisplay_Click event will display all folders and files under some the path.
ButtonUp_Click event will return parents path of this path.
ListBoxFiles_SelectedIndexChanged event will display all information of chose files.
We then call to display file properties from our ASP.NET coded page.
| Protected Property currentFolderPath() As String Get End PropertyIf Not (ViewState("m_currentFolderPath") Is Nothing) Then End GetReturn ViewState("m_currentFolderPath").ToString().Trim() Else Return String.Empty End IfSet(ByVal value As String) ViewState("m_currentFolderPath") = value End SetProtected Sub buttonDisplay_Click(ByVal sender As Object, ByVal e As EventArgs) Try End SubDim folderPath As String = txtBoxInput.Text Catch ex As Exception Dim theFolder As New DirectoryInfo(folderPath) If theFolder.Exists Then DisplayFolderList(theFolder.FullName) End IfReturn Dim theFile As New FileInfo(folderPath) If theFile.Exists Then DisplayFolderList(theFile.Directory.FullName) End IfReturn Throw New FileNotFoundException("There is no file or folder with " + "this name: " + txtBoxInput.Text) Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>")) End TryMe.ClearAllFields() Protected Sub DisplayFileInfo(ByVal fileFullName As String) Dim theFile As New FileInfo(fileFullName) End SubIf Not theFile.Exists Then Throw New FileNotFoundException("File not found: " + fileFullName) End IftxtBoxFileName.Text = theFile.Name txtBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString() txtBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString() txtBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString() txtBoxFileSize.Text = theFile.Length.ToString() + " bytes" Protected Sub DisplayFolderList(ByVal folderFullName As String) Dim theFolder As New DirectoryInfo(folderFullName) End SubIf Not theFolder.Exists Then Throw New DirectoryNotFoundException("Folder not found: " + folderFullName) End IfClearAllFields() txtBoxFolder.Text = theFolder.FullName currentFolderPath = theFolder.FullName Dim nextFolder As DirectoryInfo For Each nextFolder In theFolder.GetDirectories() listBoxFolders.Items.Add(nextFolder.Name) Next nextFolderDim nextFile As FileInfo For Each nextFile In theFolder.GetFiles() listBoxFiles.Items.Add(nextFile.Name) Next nextFileProtected Sub ClearAllFields() listBoxFolders.Items.Clear() End SublistBoxFiles.Items.Clear() txtBoxFolder.Text = "" txtBoxFileName.Text = "" txtBoxCreationTime.Text = "" txtBoxLastAccessTime.Text = "" txtBoxLastWriteTime.Text = "" txtBoxFileSize.Text = "" Protected Sub listBoxFolders_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Try End SubDim selectedString As String = listBoxFolders.SelectedItem.ToString() Catch ex As Exception Dim fullPathName As String = Path.Combine(currentFolderPath, selectedString) DisplayFolderList(fullPathName) Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>")) End TryProtected Sub listBoxFiles_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Try End SubDim selectedString As String = listBoxFiles.SelectedItem.ToString() Catch ex As Exception Dim fullFileName As String = Path.Combine(currentFolderPath, selectedString) DisplayFileInfo(fullFileName) Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>")) End TryProtected Sub buttonUp_Click(ByVal sender As Object, ByVal e As EventArgs) Try End Sub Dim folderPath As String = New FileInfo(currentFolderPath).DirectoryName Catch ex As Exception If Not (folderPath Is Nothing) Then DisplayFolderList(folderPath) Else ClearAllFields() End IfResponse.Write("<script language='javascript'>window.alert('Folder not found ');</script>") Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>")) End Try |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
The front end DisplayFilePropertiesVB.aspx page looks something like this:
| <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc"> <tr> </table> <td bgcolor="#eeeeee" class="header1"> <fieldset> <legend>DisplayFilePropertiesVB</legend> <div> <asp:Label ID="Label1" runat="server" Text="Enter name of folder to be examined and click Display"></asp:Label><br /> <asp:TextBox ID="txtBoxInput" runat="server" Width="451px"></asp:TextBox> <asp:Button ID="buttonDisplay" runat="server" Text="Display" OnClick="buttonDisplay_Click" /><br /> <fieldset> <legend>Contents of folder</legend> <asp:TextBox ID="txtBoxFolder" runat="server" Width="445px"></asp:TextBox> <asp:Button ID="buttonUp" runat="server" Text="Up" Width="66px" OnClick="buttonUp_Click" /><br /> <table> <tr> <td> <asp:Label ID="Label2" runat="server" Text="Folders"></asp:Label></td> <td> <asp:Label ID="Label3" runat="server" Text="Files"></asp:Label></td> </tr> <tr> <td> <asp:ListBox ID="listBoxFolders" runat="server" Height="150px" Width="250px" OnSelectedIndexChanged="listBoxFolders_SelectedIndexChanged" AutoPostBack="True"></asp:ListBox></td> <td> <asp:ListBox ID="listBoxFiles" runat="server" Height="150px" Width="260px" OnSelectedIndexChanged="listBoxFiles_SelectedIndexChanged" AutoPostBack="True"></asp:ListBox></td> </tr> </table> <fieldset style="width: 507px"> <legend>Details of Selected File</legend> <table> <tr> <td colspan="2"> <asp:Label ID="Label4" runat="server" Text="File name"></asp:Label><asp:TextBox ID="txtBoxFileName" runat="server" Width="429px"></asp:TextBox></td> </tr> <tr> <td> <asp:Label ID="Label5" runat="server" Text="File Size"></asp:Label></td> <td> <asp:Label ID="Label6" runat="server" Text="Creation time"></asp:Label></td> </tr> <tr> <td> <asp:TextBox ID="txtBoxFileSize" runat="server"></asp:TextBox></td> <td> <asp:TextBox ID="txtBoxCreationTime" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Label ID="Label7" runat="server" Text="Last modification time"></asp:Label></td> <td> <asp:Label ID="Label8" runat="server" Text="Last access time"></asp:Label></td> </tr> <tr> <td> <asp:TextBox ID="txtBoxLastWriteTime" runat="server"></asp:TextBox></td> <td> <asp:TextBox ID="txtBoxLastAccessTime" runat="server"></asp:TextBox></td> </tr> </table> </fieldset> </fieldset> </div> </fieldset> </td> </tr> |
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
The flow for the code behind page is as follows.
| Imports System.IO Partial Class DisplayFilePropertiesVB Inherits System.Web.UI.Page Protected Property currentFolderPath() As String Get If Not (ViewState("m_currentFolderPath") Is Nothing) Then End GetReturn ViewState("m_currentFolderPath").ToString().Trim() Else Return String.Empty End IfSet(ByVal value As String) ViewState("m_currentFolderPath") = value End PropertyEnd Set Protected Sub buttonDisplay_Click(ByVal sender As Object, ByVal e As EventArgs) Try End SubDim folderPath As String = txtBoxInput.Text Catch ex As Exception Dim theFolder As New DirectoryInfo(folderPath) If theFolder.Exists Then DisplayFolderList(theFolder.FullName) End IfReturn Dim theFile As New FileInfo(folderPath) If theFile.Exists Then DisplayFolderList(theFile.Directory.FullName) End IfReturn Throw New FileNotFoundException("There is no file or folder with " + "this name: " + txtBoxInput.Text) Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>")) End TryMe.ClearAllFields() Protected Sub DisplayFileInfo(ByVal fileFullName As String) Dim theFile As New FileInfo(fileFullName) End SubIf Not theFile.Exists Then Throw New FileNotFoundException("File not found: " + fileFullName) End IftxtBoxFileName.Text = theFile.Name txtBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString() txtBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString() txtBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString() txtBoxFileSize.Text = theFile.Length.ToString() + " bytes" Protected Sub DisplayFolderList(ByVal folderFullName As String) Dim theFolder As New DirectoryInfo(folderFullName) End SubIf Not theFolder.Exists Then Throw New DirectoryNotFoundException("Folder not found: " + folderFullName) End IfClearAllFields() txtBoxFolder.Text = theFolder.FullName currentFolderPath = theFolder.FullName Dim nextFolder As DirectoryInfo For Each nextFolder In theFolder.GetDirectories() listBoxFolders.Items.Add(nextFolder.Name) Next nextFolderDim nextFile As FileInfo For Each nextFile In theFolder.GetFiles() listBoxFiles.Items.Add(nextFile.Name) Next nextFileProtected Sub ClearAllFields() listBoxFolders.Items.Clear() End SublistBoxFiles.Items.Clear() txtBoxFolder.Text = "" txtBoxFileName.Text = "" txtBoxCreationTime.Text = "" txtBoxLastAccessTime.Text = "" txtBoxLastWriteTime.Text = "" txtBoxFileSize.Text = "" Protected Sub listBoxFolders_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Try End SubDim selectedString As String = listBoxFolders.SelectedItem.ToString() Catch ex As Exception Dim fullPathName As String = Path.Combine(currentFolderPath, selectedString) DisplayFolderList(fullPathName) Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>")) End TryProtected Sub listBoxFiles_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Try End SubDim selectedString As String = listBoxFiles.SelectedItem.ToString() Catch ex As Exception Dim fullFileName As String = Path.Combine(currentFolderPath, selectedString) DisplayFileInfo(fullFileName) Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>")) End TryProtected Sub buttonUp_Click(ByVal sender As Object, ByVal e As EventArgs) Try End SubDim folderPath As String = New FileInfo(currentFolderPath).DirectoryName Catch ex As Exception If Not (folderPath Is Nothing) Then DisplayFolderList(folderPath) Else ClearAllFields() End IfResponse.Write("<script language='javascript'>window.alert('Folder not found ');</script>") Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>")) End TryEnd Class |
No comments:
Post a Comment