Get information about a disk drive such as size and available space in C#

This example returns information about disk drives including:

  • Name
  • Total size
  • Total free size
  • Available free space
  • Format
  • Type (fixed or DVD)
  • Is ready?
  • Root directory
  • Volume label

The program uses objects in the System.IO namespace, so it includes the following statement to make using that namespace easier.

using System.IO;

When the program starts, it uses the following code to add the drives available on the system to the ComboBox cboDrive.

// Make a list of drives.
private void Form1_Load(object sender, EventArgs e)
{
    foreach (DriveInfo di in DriveInfo.GetDrives())
    {
        cboDrive.Items.Add(di.Name);
        cboDrive.SelectedIndex = 0;
    }
}

When you select a drive, the following code displays information about it.

// Display information about the selected drive.
private void cboDrive_SelectedIndexChanged(object sender, EventArgs e)
{
    string drive_letter = cboDrive.Text.Substring(0, 1);
    DriveInfo di = new DriveInfo(drive_letter);
    lblIsReady.Text = di.IsReady.ToString();
    lblDriveType.Text = di.DriveType.ToString();
    lblName.Text = di.Name;
    lblRootDirectory.Text = di.RootDirectory.Name;
    if (di.IsReady)
    {
        lblDriveFormat.Text = di.DriveFormat;
        lblAvailableFreeSpace.Text = di.AvailableFreeSpace.ToString();
        lblTotalFreeSize.Text = di.TotalFreeSpace.ToString();
        lblTotalSize.Text = di.TotalSize.ToString();
        lblVolumeLabel.Text = di.VolumeLabel;
    }
    else
    {
        lblDriveFormat.Text = "";
        lblAvailableFreeSpace.Text = "";
        lblTotalFreeSize.Text = "";
        lblTotalSize.Text = "";
        lblVolumeLabel.Text = "";
    }
}

This code makes a DriveInfo object representing the selected drive and then uses that object’s properties to learn about the drive. The only trick here is that the code checks the drive’s IsReady property before accessing some of its other properties. Some of those properties throw exceptions if the drive isn’t ready. (For example, if the drive is an empty DVD drive.)






This entry was posted in files, system and tagged , , , , , , , , , , . Bookmark the permalink.

11 Responses to Get information about a disk drive such as size and available space in C#

  1. harry says:

    how to add the drives in the drop box?

    • RodStephens says:

      If you mean, “How do you add drives to the combobox?” that’s what the example demonstrates.

      If you mean how do you search a dropbox folder as in dropbox.com, you can use normal file searching tools such as System.IO.Directory.GetFiles and System.IO.Directory.GetDirectories to search dropbox folders. Just start at the user’s dropbox root as in C:\Users\Harry\Dropbox.

  2. tommy says:

    hi, how can i get those information in a progress bar ( like windows progress bar that shows used space ) ?

  3. vishal says:

    what is lblavailablefreespace .text upto last it is lablel or textbox my output display in label format how i can i display in textbox
    please very clearity post now help me
    please give me clear solution here my code run but output dispaly in label not in textbox

    • RodStephens says:

      Yes, lblAvailableFreeSpace is a Label. To use a TextBox, just add a TextBox to the form. Suppose you call it txtAvailable. Then instead of doing this:

      lblAvailableFreeSpace.Text = di.AvailableFreeSpace.ToString();

      Do this:

      txtAvailable.Text = di.AvailableFreeSpace.ToString();
  4. Jay says:

    Hi, Is there a way to change the total size / available size fields to something that is more readable. ie. 1TB, 250GB, 100GB etc

  5. Abo Homam says:

    The tool is very nice and is working well, thanks

  6. Aousaf rashid says:

    There are some drawbacks here…..Suppose i want to get total HDD size, how would i get that ?Now, if i consider your code and make necessary changes, this is what we might get :

    public string GetTotalDisk()
    {
        int totalSize;
        DriveInfo[] allDrives = DriveInfo.GetDrives();
        DriveInfo d;
        foreach (var d in allDrives)
        {
            if (d.IsReady == true)
                totalSize += d.TotalSize / (double)(1024 * 1024 * 1024);
        }
        if (totalSize.ToString().Length >= 4)
            return totalSize.ToString() + "TB";
        else if (totalSize.Tostring().Length <= 3)
            return totalSize.ToString() + "GB";
    }

    The issue here is that if you have a 500GB HDD , it will return the result of HOW MUCH SPACE OF THE HDD YOU CAN USE instead of returning the TOTAL AMOUNT OF SPACE IT HAS … Any fix or workaround ?

    • RodStephens says:

      That’s the way people generally treat disk drives. For example, if you look at a drive in File Explorer, it displays the amount of accessible space on the drive not its theoretical possible capacity. For example, my drive says 917 GB even though it’s a 1 TB drive.

      I think that’s because drives typically have dead sectors that you cannot access in any case.

      Also note that disk drive manufacturers consider a terabyte to be 1,000,000,000 not 1024^3, so for example a 1 TB drive would only hold about 1,000,000,000 = 954 * 1024^3 bytes. The means the drive manufacturer will tell you it’s a 1 TB drive but File Explorer would only show a capacity of 953 GB.

      You can see both of those effects in this picture:


      [C# Helper]

      If you really want to know the drive’s theoretical maximum capacity, you’ll have to guess. If the system reports it’s close to 1 TB, then it’s probably a 1 TB drive.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.