-1

I am creating an installer/uninstaller but I want to make something like

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

If anyone knows how to do it please send it because i cant find the solution anywhere

  • 1
    [DriveInfo](https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.-ctor) – Jimi Sep 19 '20 at 17:40
  • Also: https://stackoverflow.com/questions/3210010/how-to-calculate-free-disk-space & https://stackoverflow.com/questions/14465187/get-available-disk-free-space-for-a-given-path-on-windows & https://stackoverflow.com/questions/1393711/get-free-disk-space –  Sep 19 '20 at 17:47
  • After seeing in details these answers they indicate `GetDrives` for most. Here you can directly use `GetDrive(driveNamePath)`. No need to get all and loop. –  Sep 19 '20 at 17:52
  • And 750 MiB (MiB I suppose, neither Mib nor MB nor Mb) = 750 * 1024 * 1024. https://en.wikipedia.org/wiki/Mebibyte –  Sep 19 '20 at 18:00

1 Answers1

1

Microsoft .NET documentation - DriveInfo.AvailableFreeSpace Property

You better use using System.IO namespace

using System.IO;

Inside main

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

The implementation of ConvertBytesToMegabytes

static double ConvertBytesToMegabytes(long bytes)
    {
        return (bytes / 1024f) / 1024f;
    }