Search code examples
c#foreachdriveinfo

Check if there is more than x bytes on ANY drive c#


I'm kinda new to this so I'll just get to it. I'm trying to figure out how to check if ANY drive has 30 GB disk space, So far I can't seem to get it to do more than just checking the C: drive.

Probably has to do with the fact that CopyAvailableCheck() only checks the first value it gets, which is from the C: drive, but I have no clue how to fix that.

Any help would be much appreciated. Here is my code:

public class DriveCheck
{
   private void CopyAvailableCheck()
   {
        if (FreeDriveSpace() == 1)
        {
          // do something
        }     
        else if (FreeDriveSpace() == 0)
        {
            // Something Else

        }
        else if (FreeDriveSpace() == -1)
        {
            // Something else

        }
   }  

   public static int FreeDriveSpace()
   {
        DriveInfo[] allDrives = DriveInfo.GetDrives();
        foreach (DriveInfo d in allDrives)
        {
            if (d.IsReady == true)
            {
                // If total free space is more than 30 GB (default)
                if (d.TotalFreeSpace >= 32212254720) // default: 32212254720
                {
                    return 1; // If everything is OK it returns 1
                }
                else
                {
                    return 0; // Not enough space returns 0
                }
            }

        }
        return -1; // Other error returns -1
    }
}

Solution

  • It won't check multiple drives because you are returning from the method inside the loop that checks the disk.

    You need to change the result of the method to be an object that can be consume by the caller that shows the answer for each drive.

    Try something like this...

    private void CopyAvailableCheck()
    {
        var listOfDisks = FreeDriveSpace();
    
        foreach( var disk in listOfDisks )
        {
            if ( disk.Status == 1)
            {
             // do something
            }
            else if ( disk.Status = 0 )
            {
              //do something else
            }
        }
    }
    
    public static List<Disk> FreeDriveSpace()
    {
        DriveInfo[] allDrives = DriveInfo.GetDrives();
        var listOfDisks = new List<Disk>();
    
        foreach (DriveInfo d in allDrives)
        {
            var currentDisk = new Disk( d.Name );   
            if (d.IsReady == true)
            {
                // If total free space is more than 30 GB (default)
                if (d.TotalFreeSpace >= 32212254720) // default: 32212254720
                {
                    currentDisk.Status = 1;
                }
                else
                {
                    currentDisk.Status = 0; // Not enough space
                }
            }
            listOfDisks.Add( currentDisk );
        }
    
        return listOfDisks;  
    }
    
    
    public class Disk
    {
        public Disk( string name )
        {
            Name = name;
        }
    
        public string Name
        {
            get; set;
        }
    
        public int Status
        {
            get; set;
        }
    }
    

    Hope this helps.

    This wasn't written in VS, it might not be perfect.