实现效果:
知识运用:
DriveInfo类的TotalSize属性
public long TotalSize {get;} //获取驱动器上的存储空间的总大小
TotalFreeSpace属性
public long TotalFreeSpace {get;} //获取驱动器上的空用空间总量
实现代码:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { System.IO.DriveInfo[] drive = System.IO.DriveInfo.GetDrives(); foreach (System.IO.DriveInfo s in drive) { if (comboBox1.SelectedItem.ToString() == s.Name) { label2.Text="总量:"+s.TotalSize/1024/1024/1024+"G"; label3.Text="剩余:"+s.TotalFreeSpace/1024/1024/1024+"G"; label4.Text="已用:"+(s.TotalSize-s.TotalFreeSpace)/1024/1024/1024+"G"; } } }