Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - File - Display file property using ASP.NET 2.0 and VB .NET
Display file property using ASP.NET 2.0 and VB .NET


ASP.NET File Tutorial

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.

Untitled Document

Download the Full Working Version of this Project written with Visual Studio.NET VB 2005 Here!

Looking for more ASP.NET Tutorials? Click Here!


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

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>

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

Download the Full Working Version of this Project written with Visual Studio.NET VB 2005 Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks