DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Display drive information using ASP.NET 2.0 and C#.

This example is display drive information using DriveInfo Class. Display drive information using ASP.NET 2.0 and C#.

This example is display drive information using DriveInfo Class.

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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.

using System.IO

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.

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DriveInfo[] di = DriveInfo.GetDrives();
foreach (DriveInfo item in di)
{
listboxDrive.Items.Add(item.Name);
}
}
}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string driveName = listboxDrive.SelectedItem.ToString();
DriveInfo di = 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>");
}
}

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>

The flow for the code behind page is as follows.

using System; using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class DisplayDriveInfoCsharp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DriveInfo[] di = DriveInfo.GetDrives();
foreach (DriveInfo item in di)
{
listboxDrive.Items.Add(item.Name);
}
}
}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string driveName = listboxDrive.SelectedItem.ToString();
DriveInfo di = 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>");
}
}
}

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!




Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!