Search Web

Friday, August 19, 2011

How To Read Local Drive Information With Asp.net Application

Some time we need to interact with the local file system, reading structure of the Directories, reading and writing files, or performing many other jobs.

In this Application we need use System.IO Namespace for working with file system directories. System.IO Contain Driveinfo class.


Driveinfo class is used to get information such us the name,type, and status of each drive.

DriveInfo DriveDetails= new DriveInfo(@"C:\");

DriveDetails.Name : it is used to get the name of the specified drive.

DriveDetails.DriveType : it is used to get type of the specified drive.

DriveDetails.AvailableFreeSpace : it is used to get the available free space in the specified drive.

DriveDetails.TotalFreeSpace :it is used to get the total free space in the specified drive.

DriveDetails.TotalSize :it is used to get the total size in the specified drive.

DriveDetails.DriveFormat : it is used to get the format of the specified drive.

Here I am Getting C drive Information such us Drive Name, Drive Type, Free Space, Drive Format, Total Free Space, Total Size.

Here i am getting drive Size is in bytes. but i want result in Giga Bytes.

1024 Bytes = 1 KiloByte(KB)
1024 KiloBytes(KB) = 1 MegaByte(MB)
1024 MegaBytes(MB) = 1 GigaByte(GB)
1024 GigaBytes(GB) = 1 TeraByte(TB)

I am converting Bytes to Giga bytes.

double db = Convert.ToDouble(lblTotalSize.Text);
double db1 = db / 1024;
double db2 = db1 / 1024;
int db3 = Convert.ToInt32(db2) / 1024;


protected void Page_Load(object sender, EventArgs e)
    {
        DriveInfo DriveDetails= new DriveInfo(@"C:\");
        lblDriveName.Text = DriveDetails.Name;
        lblDriveType.Text = DriveDetails.DriveType.ToString();
        lblFreeSpace.Text = DriveDetails.AvailableFreeSpace.ToString();
        lblDriveFormat.Text = DriveDetails.DriveFormat.ToString();
        lblTotalFreeSpace.Text = DriveDetails.TotalFreeSpace.ToString();
        lblTotalSize.Text = DriveDetails.TotalSize.ToString();
        double db = Convert.ToDouble(lblTotalSize.Text);
        double db1 = db / 1024;
        double db2 = db1 / 1024;
        int db3 = Convert.ToInt32(db2) / 1024;
        lblTotalSizeInGb.Text = db3.ToString() + " GB";

        double TS = Convert.ToDouble(lblTotalFreeSpace.Text);
        double TS1 = TS / 1024;
        double TS2 = TS1 / 1024;
        int TS3 = Convert.ToInt32(TS2) / 1024;
        lblTotalFreeSpaceGB.Text = TS3.ToString() + " GB";

    }

Now you can understand about How to read Drive information from local System using Asp.Net Web application AND to calculate Bytes to Giga bytes in Web Application

If you find a bug and have a fix for it: please let me know and I'll be happy to update the code I am hosting here.

Any suggestions will be appreciated greatly.

0 comments:

Post a Comment