//获取计算机上所有逻辑驱动器
DriveInfo[] allDrives = DriveInfo.GetDrives();
//遍历所有逻辑驱动器
foreach (DriveInfo d in allDrives)
{
//过滤掉非固定磁盘(如光驱、移动设备等)
if (d.DriveType == DriveType.Fixed)
{
//盘符名
string name = d.Name;
//剩余空间百分比,保留2位小数
double freeSpacePercent = Math.Round(((double)d.TotalFreeSpace / d.TotalSize) * 100, 2);
//输出文本
string textContent = name + "盘剩余可用空间:" + freeSpacePercent + "%";
}
}