Display Drivers Information
In this article I'm trying to explain how to display our system drivers' information into our application. How to check size of that and how to check driver type and how to check free space and available space of that.
Display Drivers Information :
In this article I'm trying to explain how to display our system drivers' information into our application. How to check size of that and how to check driver type and how to check free space and available space of that.
Follow below steps to achieve your goal.Step-1:
Create a project and right click on solution explorer and then choose Add -> New Item, then choose WebForm and give a name for that as "RetrieveDriverInfo.aspx".Step-2:
After create web page design a page like below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RetrieveDriverInfo.aspx.cs" Inherits="ASPnet_RetrieveDriverInfo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
color: #000066;
font-family: "Trebuchet MS";
font-size: large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<span class="style1"><strong>Display Drivers Information<br />
</strong></span>
<br />
<table width="80%">
<tr>
<td>
<asp:Button ID="btnShow" runat="server" Text="Display Drivers"
onclick="btnShow_Click" />
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlDrivers" runat="server" OnSelectedIndexChanged="ddlDrivers_OnSelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblSelect" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblType" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblSize" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblSpace" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>Step-3:
After design a page, just double click on button control then it's redirect to code behind button click event then bind total drivers information into one dropdown control.
protected void btnShow_Click(object sender, EventArgs e)
{
//GetLogicalDrivers method is used to the drivers available in the system
string[] str = System.IO.Directory.GetLogicalDrives();
//assign list of drivers into dropdown control
for (int i = 0; i < str.Length; i++)
{
ddlDrivers.Items.Add(str[i]);
}
ddlDrivers.Items.Insert(0, "-Select-");
}Step-4:
Once bind the drivers' information into dropdown, then each and every driver selection you can fetch that particular driver information like size, type, space etc…
protected void ddlDrivers_OnSelectedIndexChanged(object sender, EventArgs e)
{
try
{
lblSelect.Text = "You have selected Driver : " + ddlDrivers.SelectedValue;
System.IO.DriveInfo obj=new System.IO.DriveInfo(ddlDrivers.SelectedValue);
lblType.Text = "Driver Type : " + obj.DriveType.ToString();
lblSpace.Text = "Available Space : " + obj.AvailableFreeSpace.ToString();
lblSize.Text = "Total Size : " + obj.TotalSize.ToString();
}
catch (Exception ex)
{
Response.Write(ex.Message);
lblType.Text = lblSpace.Text = lblSize.Text = "";
}
}Step-5:
Now execute the above code, just click F5 on keyboard then see the output on each and every selection then see the result.
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class ASPnet_RetrieveDriverInfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblSelect.Text = lblSize.Text = lblSpace.Text = lblType.Text = "";
}
}
protected void btnShow_Click(object sender, EventArgs e)
{
//GetLogicalDrivers method is used to the drivers available in the system
string[] str = System.IO.Directory.GetLogicalDrives();
//assign list of drivers into dropdown control
for (int i = 0; i < str.Length; i++)
{
ddlDrivers.Items.Add(str[i]);
}
ddlDrivers.Items.Insert(0, "-Select-");
}
protected void ddlDrivers_OnSelectedIndexChanged(object sender, EventArgs e)
{
try
{
lblSelect.Text = "You have selected Driver : " + ddlDrivers.SelectedValue;
System.IO.DriveInfo obj=new System.IO.DriveInfo(ddlDrivers.SelectedValue);
lblType.Text = "Driver Type : " + obj.DriveType.ToString();
lblSpace.Text = "Available Space : " + obj.AvailableFreeSpace.ToString();
lblSize.Text = "Total Size : " + obj.TotalSize.ToString();
}
catch (Exception ex)
{
Response.Write(ex.Message);
lblType.Text = lblSpace.Text = lblSize.Text = "";
}
}
}Conclusion:
Hope this article will help you those who are new to Drivers selection process, this is basic information to learn how to perform operation using C# code and display the drivers' information into our application.