首页 > 解决方案 > 如何检查可用磁盘空间的最小量C#

问题描述

我正在创建一个安装程序/卸载程序,但我想做类似的东西

if(disk.c.freespace == 750mb)
  {
  continue my program stuff
  }
else
  {
  this.text = ("Error!")
  }

如果有人知道该怎么做,请发送它,因为我无法在任何地方找到解决方案

标签: c#

解决方案


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;
    }

推荐阅读