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 - Advanced Technologies - Display drive information using ASP.NET 2.0 and C#.
Display drive information using ASP.NET 2.0 and C#.


ASP.NET Advanced Technologies Tutorial

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.

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 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 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 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>");
}
}

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

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>

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

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>");
}
}
}

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

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

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks