获取电脑磁盘剩余空间

static void Main(string[] args)
{
Console.WriteLine("磁盘剩余空间");
System.IO.DriveInfo[] allDrives = System.IO.DriveInfo.GetDrives();
foreach (var item in allDrives)
{
Console.Write(item.ToString().Replace(":\","")+":");
Console.WriteLine(GetHardDiskFreeSpace(item.ToString().Replace(":\", "")).ToString("0.00")+"G");
}
Console.ReadKey();
}
///
/// 获取指定驱动器的剩余空间总大小(单位为B)
///
/// 只需输入代表驱动器的字母即可
///
public static double GetHardDiskFreeSpace(string str_HardDiskName)
{
double freeSpace = new double();
str_HardDiskName = str_HardDiskName + ":\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == str_HardDiskName)
{
freeSpace = drive.TotalFreeSpace / (1024 * 1024 * 1024.00);
}
}
return freeSpace;
}

原文地址:https://www.cnblogs.com/yangdunqin/p/disk.html