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> <fieldset> <legend>Drive List</legend> </fieldset><asp:ListBox ID="listboxDrive" runat="server" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" AutoPostBack="True" Width="480px"></asp:ListBox> <legend>Details of Selected Drive</legend> </fieldset></div> <table> <tr> </table><td style="width: 183px"> </tr><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> <td style="width: 183px"> </tr><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> <td style="width: 183px"> </tr><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> <td style="width: 183px"> </tr><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> <td style="width: 183px"> </tr><asp:Label ID="Label5" runat="server">Name:</asp:Label></td> <td style="width: 283px"> <asp:Label ID="labName" runat="server"></asp:Label></td> <tr> <td style="width: 183px"> </tr><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> <td style="width: 183px"> </tr><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> <td style="width: 183px"> </tr><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> <td style="width: 183px"> </tr><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> <td style="width: 183px"> </tr><asp:Label ID="Label10" runat="server">Volume Label:</asp:Label></td> <td style="width: 283px"> <asp:Label ID="labVolume" runat="server"></asp:Label></td> |
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>"); |
No comments:
Post a Comment