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 CD ROM
- Is ready?
- Root directory
- Volume label
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.When you select a drive, the following code displays information about it.
private void Form1_Load(object sender, EventArgs e)
{
foreach (DriveInfo di in DriveInfo.GetDrives())
{
cboDrive.Items.Add(di.Name);
cboDrive.SelectedIndex = 0;
}
}
// Display information about the selected drive.The program makes a DriveInfo object representing the selected drive and uses that object's properties to learn about the 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 = "";
}
}



Comments