Tuesday, 19 July 2011

Display drive information using ASP.NET 2.0 and VB.NET


This example is display drive information using DriveInfo Class.

This example is display drive information using DriveInfo Class.

First, you will need to import the System.IO namespace.

The System.IO namespace contains the DriveInfo Class that provides access to information on a drive.This class models a drive and provides methods and properties to query for drive information. Use DriveInfo to determine what drives are available, and what type of drives they are. You can also query to determine the capacity and available free space on the drive.

Imports System.IO

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

We use the Page_Load event to provide the list of drives .And use the ListBox1_SelectedIndexChanged event to provide information on a drive.

The code as follows.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim di As DriveInfo() = DriveInfo.GetDrives()
Dim item As DriveInfo
For Each item In di
listboxDrive.Items.Add(item.Name)
Next item
End If
End Sub

Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim driveName As String = listboxDrive.SelectedItem.ToString()
Dim di As New DriveInfo(driveName)
Try
labAvailableFreeSpace.Text = di.AvailableFreeSpace.ToString()
labFormat.Text = di.DriveFormat.ToString()
labType.Text = di.DriveType.ToString()
labReady.Text = di.IsReady.ToString()
labName.Text = di.Name.ToString()
labRootDirectory.Text = di.RootDirectory.ToString()
labValue.Text = di.ToString()
labFreeSpace.Text = di.TotalFreeSpace.ToString()
labTotalSize.Text = di.TotalSize.ToString()
labVolume.Text = di.VolumeLabel.ToString()
Catch
Response.Write("<script language='javascript'>window.alert('The device is not ready');</script>")
Response.Write("<script language='javascript'>history.go(-1);</script>")
End Try
End Sub

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!

The front end DisplayDriveInfoCsharp.aspx page looks something like this:

<div>
<fieldset>
<legend>Drive List</legend>
<asp:ListBox ID="listboxDrive" runat="server" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" AutoPostBack="True" Width="480px"></asp:ListBox>
</fieldset>
<fieldset>
<legend>Details of Selected Drive</legend>
<table>
<tr>
<td style="width: 183px">
<asp:Label ID="Label1" runat="server">Available Free Space:</asp:Label></td>
<td style="width: 283px">
<asp:Label ID="labAvailableFreeSpace" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width: 183px">
<asp:Label ID="Label2" runat="server">Drive Format:</asp:Label></td>
<td style="width: 283px">
<asp:Label ID="labFormat" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width: 183px">
<asp:Label ID="Label3" runat="server">Drive Type:</asp:Label></td>
<td style="width: 283px">
<asp:Label ID="labType" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width: 183px">
<asp:Label ID="Label4" runat="server">Is Ready:</asp:Label></td>
<td style="width: 283px">
<asp:Label ID="labReady" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width: 183px">
<asp:Label ID="Label5" runat="server">Name:</asp:Label></td>
<td style="width: 283px">
<asp:Label ID="labName" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width: 183px">
<asp:Label ID="Label6" runat="server">Root Directory:</asp:Label></td>
<td style="width: 283px">
<asp:Label ID="labRootDirectory" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width: 183px">
<asp:Label ID="Label7" runat="server">ToString() Value:</asp:Label></td>
<td style="width: 283px">
<asp:Label ID="labValue" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width: 183px">
<asp:Label ID="Label8" runat="server">Total Free Space:</asp:Label></td>
<td style="width: 283px">
<asp:Label ID="labFreeSpace" runat="server"></asp:Label>/td>
</tr>
<tr>
<td style="width: 183px">
<asp:Label ID="Label9" runat="server">Total Size:</asp:Label></td>
<td style="width: 283px">
<asp:Label ID="labTotalSize" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width: 183px">
<asp:Label ID="Label10" runat="server">Volume Label:</asp:Label></td>
<td style="width: 283px">
<asp:Label ID="labVolume" runat="server"></asp:Label></td>
</tr>
</table>
</fieldset></div>

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The flow for the code behind page is as follows.

Imports System.IO

Partial Class DisplayDriveInfoVB
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim di As DriveInfo() = DriveInfo.GetDrives()
Dim item As DriveInfo
For Each item In di
listboxDrive.Items.Add(item.Name)
Next item
End If
End Sub

Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim driveName As String = listboxDrive.SelectedItem.ToString()
Dim di As New DriveInfo(driveName)
Try
labAvailableFreeSpace.Text = di.AvailableFreeSpace.ToString()
labFormat.Text = di.DriveFormat.ToString()
labType.Text = di.DriveType.ToString()
labReady.Text = di.IsReady.ToString()
labName.Text = di.Name.ToString()
labRootDirectory.Text = di.RootDirectory.ToString()
labValue.Text = di.ToString()
labFreeSpace.Text = di.TotalFreeSpace.ToString()
labTotalSize.Text = di.TotalSize.ToString()
labVolume.Text = di.VolumeLabel.ToString()
Catch
Response.Write("<script language='javascript'>window.alert('The device is not ready');</script>")
Response.Write("<script language='javascript'>history.go(-1);</script>")
End Try
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment