c# - 如何检查可用磁盘空间的最小量C#
问题描述
我正在创建一个安装程序/卸载程序,但我想做类似的东西
if(disk.c.freespace == 750mb)
{
continue my program stuff
}
else
{
this.text = ("Error!")
}
如果有人知道该怎么做,请发送它,因为我无法在任何地方找到解决方案
解决方案
Microsoft .NET 文档 - DriveInfo.AvailableFreeSpace 属性
您最好使用 using System.IO 命名空间
using System.IO;
内部主要
DriveInfo[] allDrives = DriveInfo.GetDrives();
// get the correct hard drive
for(int i = 0; i < allDrives.Length; i++)
{
if (allDrives[i].Name == "C:\\")
{
if (ConvertBytesToMegabytes(allDrives[i].TotalFreeSpace) == 750)
{
Console.WriteLine("Success");
} else
{
Console.WriteLine("Error");
}
}
}
ConvertBytesToMegabytes 的实现
static double ConvertBytesToMegabytes(long bytes)
{
return (bytes / 1024f) / 1024f;
}
推荐阅读
- php - 如何循环遍历 N 级子子数组 - PHP
- java - 如何在安装页面后只运行一次该页面
- android - 清单合并失败并出现多个错误,生成签名 apk 时查看日志
- python - 如何使用python将一张图像的相位和不同图像的大小组合成一张图像
- javascript - 在 MVC 中调用 AJAX 后,选择框的 JavaScript 文件不起作用
- c++ - cassandra cpp 驱动程序 dev 2.9.0-1 不会安装在 Debian Stretch 上?
- ios - 定制 B2B 和批量购买计划的成本是多少?
- javascript - 为什么我不能使用 Array.prototype.find 和 String.prototype.startsWith 作为过滤器?
- elasticsearch - 按共享字段值对聚合桶进行排序
- sql - 使用 SQL 查询的 XML 报告