https://blog.youkuaiyun.com/BYH371256/article/details/89226299
基于上面的资料进行了小小的改动
//总
public static double GetDiskSize(string diskName)
{
try
{
return Math.Round(Convert.ToDouble((from x in DriveInfo.GetDrives() where x.Name == $"{diskName}:\\" select x.TotalSize).SingleOrDefault() / 1024 / 1024 / 1024), 2);
}
catch (Exception)
{
return 0;
}
}
//空闲
public static double GetDiskSize1(string diskName)
{
try
{
return Math.Round(Convert.ToDouble((from x in DriveInfo.GetDrives() where x.Name == $"{diskName}:\\" select x.TotalFreeSpace).SingleOrDefault() / 1024 / 1024 / 1024), 2);
}
catch (Exception)
{
return 0;
}
}
已使用就是总-空闲
关键点在select x.TotalFreeSpace 查询大小 单位是B 鉴于我懒 直接转换成GB