核心 API
System.IO.DriveInfo.GetDrives():取得所有邏輯磁碟機資訊陣列。
DriveInfo.TotalSize:磁碟總容量 (位元組)。
完整 WinForms 範例
private void Form1_Load(object sender, EventArgs e)
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives)
comboBox1.Items.Add(d.Name);
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives)
{
if (comboBox1.SelectedItem.ToString() == d.Name)
{
// 顯示 KB (TotalSize / 1024)
textBox1.Text = (d.TotalSize / 1024).ToString();
break;
}
}
}
補充說明
DriveInfo 還提供 AvailableFreeSpace、TotalFreeSpace、DriveFormat、DriveType 等屬性。
- 若需顯示 MB/GB,自行換算 (
/1024/1024 或 /1024/1024/1024)。
- 需參考
System.IO 命名空間。