【问题标题】:How to check the minimum amount of free disk space C# [duplicate]如何检查可用磁盘空间的最小量C# [重复]
【发布时间】:2020-09-20 02:10:34
【问题描述】:

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

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

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

【问题讨论】:

标签: c#


【解决方案1】:

Microsoft .NET documentation - DriveInfo.AvailableFreeSpace Property

你最好使用 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;
    }

【讨论】:

  • var driveC = new DriveInfo("C");
  • 让我看看它是否有效
  • 即使我在所有驱动器上剩余超过 30gb 也会返回错误
  • 你不能用双精度值进行这种比较:if (ConvertBytesToMegabytes(allDrives[i].TotalFreeSpace) == 750)。至少将其更改为&gt;=
  • 在 750mb 时返回成功,当我将其设为 75gb 时返回错误,作为一种魅力,谢谢伙计
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多