Monday, 25 July 2011

Display file property 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.

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
If Not (ViewState("m_currentFolderPath") Is Nothing) Then
Return ViewState("m_currentFolderPath").ToString().Trim()
Else
Return String.Empty
End If
End Get
Set(ByVal value As String)
ViewState("m_currentFolderPath") = value
End Set
End Property

Protected Sub buttonDisplay_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim folderPath As String = txtBoxInput.Text
Dim theFolder As New DirectoryInfo(folderPath)
If theFolder.Exists Then
DisplayFolderList(theFolder.FullName)
Return
End If
Dim theFile As New FileInfo(folderPath)
If theFile.Exists Then
DisplayFolderList(theFile.Directory.FullName)
Return
End If
Throw New FileNotFoundException("There is no file or folder with " + "this name: " + txtBoxInput.Text)
Catch ex As Exception
Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>"))
Me.ClearAllFields()
End Try
End Sub

Protected Sub DisplayFileInfo(ByVal fileFullName As String)
Dim theFile As New FileInfo(fileFullName)
If Not theFile.Exists Then
Throw New FileNotFoundException("File not found: " + fileFullName)
End If

txtBoxFileName.Text = theFile.Name
txtBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString()
txtBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString()
txtBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString()
txtBoxFileSize.Text = theFile.Length.ToString() + " bytes"
End Sub

Protected Sub DisplayFolderList(ByVal folderFullName As String)
Dim theFolder As New DirectoryInfo(folderFullName)
If Not theFolder.Exists Then
Throw New DirectoryNotFoundException("Folder not found: " + folderFullName)
End If

ClearAllFields()
txtBoxFolder.Text = theFolder.FullName
currentFolderPath = theFolder.FullName
Dim nextFolder As DirectoryInfo
For Each nextFolder In theFolder.GetDirectories()
listBoxFolders.Items.Add(nextFolder.Name)
Next nextFolder

Dim nextFile As FileInfo
For Each nextFile In theFolder.GetFiles()
listBoxFiles.Items.Add(nextFile.Name)
Next nextFile
End Sub

Protected Sub ClearAllFields()
listBoxFolders.Items.Clear()
listBoxFiles.Items.Clear()
txtBoxFolder.Text = ""
txtBoxFileName.Text = ""
txtBoxCreationTime.Text = ""
txtBoxLastAccessTime.Text = ""
txtBoxLastWriteTime.Text = ""
txtBoxFileSize.Text = ""
End Sub

Protected Sub listBoxFolders_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim selectedString As String = listBoxFolders.SelectedItem.ToString()
Dim fullPathName As String = Path.Combine(currentFolderPath, selectedString)
DisplayFolderList(fullPathName)
Catch ex As Exception
Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>"))
End Try
End Sub

Protected Sub listBoxFiles_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim selectedString As String = listBoxFiles.SelectedItem.ToString()
Dim fullFileName As String = Path.Combine(currentFolderPath, selectedString)
DisplayFileInfo(fullFileName)
Catch ex As Exception
Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>"))
End Try
End Sub

Protected Sub buttonUp_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim folderPath As String = New FileInfo(currentFolderPath).DirectoryName
If Not (folderPath Is Nothing) Then
DisplayFolderList(folderPath)
Else
ClearAllFields()
Response.Write("<script language='javascript'>window.alert('Folder not found ');</script>")
End If
Catch ex As Exception
Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>"))
End Try
End Sub

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>
<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>
</table>

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
Return ViewState("m_currentFolderPath").ToString().Trim()
Else
Return String.Empty
End If
End Get

Set(ByVal value As String)
ViewState("m_currentFolderPath") = value
End Set
End Property

Protected Sub buttonDisplay_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim folderPath As String = txtBoxInput.Text
Dim theFolder As New DirectoryInfo(folderPath)
If theFolder.Exists Then
DisplayFolderList(theFolder.FullName)
Return
End If

Dim theFile As New FileInfo(folderPath)
If theFile.Exists Then
DisplayFolderList(theFile.Directory.FullName)
Return
End If
Throw New FileNotFoundException("There is no file or folder with " + "this name: " + txtBoxInput.Text)
Catch ex As Exception
Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>"))
Me.ClearAllFields()
End Try
End Sub

Protected Sub DisplayFileInfo(ByVal fileFullName As String)
Dim theFile As New FileInfo(fileFullName)
If Not theFile.Exists Then
Throw New FileNotFoundException("File not found: " + fileFullName)
End If

txtBoxFileName.Text = theFile.Name
txtBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString()
txtBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString()
txtBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString()
txtBoxFileSize.Text = theFile.Length.ToString() + " bytes"
End Sub

Protected Sub DisplayFolderList(ByVal folderFullName As String)
Dim theFolder As New DirectoryInfo(folderFullName)
If Not theFolder.Exists Then
Throw New DirectoryNotFoundException("Folder not found: " + folderFullName)
End If

ClearAllFields()
txtBoxFolder.Text = theFolder.FullName
currentFolderPath = theFolder.FullName
Dim nextFolder As DirectoryInfo
For Each nextFolder In theFolder.GetDirectories()
listBoxFolders.Items.Add(nextFolder.Name)
Next nextFolder
Dim nextFile As FileInfo
For Each nextFile In theFolder.GetFiles()
listBoxFiles.Items.Add(nextFile.Name)
Next nextFile
End Sub

Protected Sub ClearAllFields()
listBoxFolders.Items.Clear()
listBoxFiles.Items.Clear()
txtBoxFolder.Text = ""
txtBoxFileName.Text = ""
txtBoxCreationTime.Text = ""
txtBoxLastAccessTime.Text = ""
txtBoxLastWriteTime.Text = ""
txtBoxFileSize.Text = ""
End Sub

Protected Sub listBoxFolders_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim selectedString As String = listBoxFolders.SelectedItem.ToString()
Dim fullPathName As String = Path.Combine(currentFolderPath, selectedString)
DisplayFolderList(fullPathName)
Catch ex As Exception
Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>"))
End Try
End Sub

Protected Sub listBoxFiles_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim selectedString As String = listBoxFiles.SelectedItem.ToString()
Dim fullFileName As String = Path.Combine(currentFolderPath, selectedString)
DisplayFileInfo(fullFileName)
Catch ex As Exception
Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>"))
End Try
End Sub

Protected Sub buttonUp_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim folderPath As String = New FileInfo(currentFolderPath).DirectoryName
If Not (folderPath Is Nothing) Then
DisplayFolderList(folderPath)
Else
ClearAllFields()
Response.Write("<script language='javascript'>window.alert('Folder not found ');</script>")
End If
Catch ex As Exception
Response.Write(("<script language='javascript'>window.alert('" + ex.Message + "');</script>"))
End Try
End Sub
End Class

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment