Friday 4 January 2013

how to get logical drives and driveinfo in asp.net using C#

DropDownlist Binding to LogicalDrives in asp.net






Step1) Create a ASP.NET Empty WebSite using C# 4.0/4.5
Step 2)  Add new Page name it as Default.aspx
Step3) Add DropDownList and Label


    add event handler for OnSelectedIndexChanged and Autopostback set to true.otheriwise post back will not occur when selection changes in DropDownList
 
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Selected="True">Select Logical Drive</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>


Step 4)  Add a Method to get LogicalDrives
        String[] GetLogicalDrives()
        {
            return System.Environment.GetLogicalDrives();
        }

Step 5) Add a method to get DriveInfo

        String GetDriveInfo(String drive)
        {
            string str = "Drive not Ready";
            try
            {
                DriveInfo dInfo = new DriveInfo(drive);
               
                if (dInfo.IsReady)
                {
                    str = "<b>AvailableFreeSpace</b>:" +Get_Size_in_KB_MB_GB((ulong)dInfo.AvailableFreeSpace,0) + "<br/>";
                    str += "<b>TotalFreeSpace</b>:" + Get_Size_in_KB_MB_GB((ulong)dInfo.TotalFreeSpace,0) + "<br/>";
                    str += "<b>TotalSize</b>:" + Get_Size_in_KB_MB_GB((ulong)dInfo.TotalSize,0) + "<br/>";
                    str += "<b>VolumeLabel</b>:" + dInfo.VolumeLabel + "<br/>";
                    str += "<b>RootDirectory</b>:" + dInfo.RootDirectory + "<br/>";
                    str += "<b>Name</b>:" + dInfo.Name + "<br/>";
                    str += "<b>DriveType</b>:" + dInfo.DriveType + "<br/>";
                    str += "<b>DriveFormat</b>:" + dInfo.DriveFormat + "<br/>";
                }
            }
            catch (Exception ex)
            {
            }
            return str;
        }

Step 6)  Bind Logical Drives Array to DropDownList

                DropDownList1.DataSource = GetLogicalDrives();
                DropDownList1.DataBind();

Step 7) Add Default value to DropDownList
                DropDownList1.Items.Insert(0,"Select Logical Drive");
Step 8) Add Code for DropDownList1_SelectedIndexChanged
            ListItem item=DropDownList1.SelectedItem;
            lblMsg.Text=GetDriveInfo(item.Text);


Step 9) Convert Bytes to KB,MB,GB in C#

        String[] sizeArry = new String[] { "Byes", "KB", "MB", "GB" };

        String Get_Size_in_KB_MB_GB(ulong sizebytes, int index)
        {

            if (sizebytes < 1000) return sizebytes + sizeArry[index];

            else return Get_Size_in_KB_MB_GB(sizebytes / 1024, ++index);

        }

Step 10) Run the WebPage  Default.aspx


how to get logical drives and driveinfo in asp.net using C# 
how to get logical drives and driveinfo in asp.net using C#




















Complete Source Code

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DropDownBox-Demo.aspx.cs" Inherits="WebApplication1.DropDownBox_Demo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1
        {
            font-size: xx-large;
            color:red;
            background-color:aqua;
        }
        b
        {
             color:teal;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>DropDownList Demo in ASP.NET</h1>
        <span class="auto-style1">Select Logical Drive:</span>:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" Height="21px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="164px" BackColor="Red" Font-Names="Times New Roman" Font-Size="Larger" ForeColor="#66FF33">
            <asp:ListItem Selected="True">Select Logical Drive</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace WebApplication1
{
    public partial class DropDownBox_Demo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DropDownList1.DataSource = GetLogicalDrives();
                DropDownList1.DataBind();
               
               
                DropDownList1.Items.Insert(0,"Select Logical Drive");
            }

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListItem item=DropDownList1.SelectedItem;
            lblMsg.Text=GetDriveInfo(item.Text);
        }

        String[] GetLogicalDrives()
        {
            return System.Environment.GetLogicalDrives();
        }

        String GetDriveInfo(String drive)
        {
            string str = "Drive not Ready";
            try
            {
                DriveInfo dInfo = new DriveInfo(drive);
               
                if (dInfo.IsReady)
                {
                    str = "<b>AvailableFreeSpace</b>:" +Get_Size_in_KB_MB_GB((ulong)dInfo.AvailableFreeSpace,0) + "<br/>";
                    str += "<b>TotalFreeSpace</b>:" + Get_Size_in_KB_MB_GB((ulong)dInfo.TotalFreeSpace,0) + "<br/>";
                    str += "<b>TotalSize</b>:" + Get_Size_in_KB_MB_GB((ulong)dInfo.TotalSize,0) + "<br/>";
                    str += "<b>VolumeLabel</b>:" + dInfo.VolumeLabel + "<br/>";
                    str += "<b>RootDirectory</b>:" + dInfo.RootDirectory + "<br/>";
                    str += "<b>Name</b>:" + dInfo.Name + "<br/>";
                    str += "<b>DriveType</b>:" + dInfo.DriveType + "<br/>";
                    str += "<b>DriveFormat</b>:" + dInfo.DriveFormat + "<br/>";
                }
            }
            catch (Exception ex)
            {
            }
            return str;
        }

        String Get_Size_in_KB_MB_GB(ulong sizebytes, int index)
        {

            if (sizebytes < 1000) return sizebytes + sizeArry[index];

            else return Get_Size_in_KB_MB_GB(sizebytes / 1024, ++index);

        }
        String[] sizeArry = new String[] { "Byes", "KB", "MB", "GB" };


    }
}

No comments:

Post a Comment