How to get Drive information in C# by using driveinfo class

Introduction

We are very familiar with drive properties in operating system, through drive properties.

We can gain important information of selected drive such as used space, free space, file system.

DriveInfo1.JPG

We can also create drive property page in C# by using Driveinfo class of System.IO namespaces.

Driveinfo2.JPG

User can determine what drives are available and capacity and available free space on the drive. System.IO.DriveInfo provides various methods and properties for handling above discussed task these are as follows.

Methods of driveInfo Class
 

Name Description
Equals(Object) Determines whether the specified Object is equal to the current Object.
Finalize Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.
GetDrives Retrieves the drive names of all logical drives on a computer.
GetHashCode Serves as a hash function for a particular type
GetType Gets the Type of the current instance
MemberwiseClone Creates a shallow copy of the current Object.
ToString Returns a drive name as a string.


Properties of driveInfo Class
 

Name Description
AvailableFreeSpace Indicates the amount of available free space on a drive.
DriveFormat Gets the name of the file system, such as NTFS or FAT32.
DriveType Gets the drive type.
IsReady Gets a value indicating whether a drive is ready.
Name Gets the name of a drive.
RootDirectory Gets the root directory of a drive.
TotalFreeSpace Gets the total amount of free space available on a drive.
TotalSize Gets the total size of storage space on a drive.
VolumeLabel Gets or sets the volume label of a drive.

Code Example

Add new window form and add some controls Labels, Groupboxes,buton(see picture).

Create load event of form, selectedindexChanged event of combox and paint event of groupbox on where chart is shown.

Now first we need to bind all available drive's name in combo box like this.

private void Form1_Load(object sender, EventArgs e)
{
    System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();

    for (int i = 0; i <= drives.Length - 1; i++)
    {
        cmbCombo.Items.Add(drives[i].Name);
    }

    cmbCombo.SelectedIndex = -1;
}

For showing various information regarding selected drive we need to write this code on SelectedIndexChanged event of combo box.

Drive_Info = new System.IO.DriveInfo(cmbCombo.Text);
lblName.Text = Drive_Info.Name;

if (Drive_Info.IsReady)
{
    if (Drive_Info.VolumeLabel.Length > 0)
    {
        lblVolumeLabel.Text = Drive_Info.VolumeLabel;
    }
    else
    {
        lblVolumeLabel.Text = "None";
    }

    lblFileSystem.Text = Drive_Info.DriveFormat;
    lblType.Text = Drive_Info.DriveType.ToString();
    lblRootDir.Text = Drive_Info.RootDirectory.FullName;
    lblCapacity.Text = Drive_Info.TotalSize + " (Bytes)";
    lblAvalspace.Text = Drive_Info.TotalFreeSpace + " (Bytes)";

    long usedSpace = 0;
    usedSpace = Drive_Info.TotalSize - Drive_Info.TotalFreeSpace;
    lblUsedSpace.Text = usedSpace + " (Bytes)";

    Piemark = 360f * Drive_Info.TotalFreeSpace / Drive_Info.TotalSize;
    long ProgressCurrentValue = usedSpace * 100 / Drive_Info.TotalSize;
    IsInfoAvailiable = true;
}

We can also use Pie chart for showing used and free space in drive by various color using paint event and FillPie method of graphices class like this.

private void GroupBox3_Paint(object sender, PaintEventArgs e)
{
    Rectangle rect = new Rectangle(130, 50, 120, 50);

    if (IsInfoAvailiable)
    {
        e.Graphics.FillPie(Brushes.Fuchsia, rect, 0, Piemark);
        e.Graphics.FillPie(Brushes.Blue, rect, Piemark, 360 - Piemark);
        Application.DoEvents();
    }
}


Similar Articles