Check drive free space.

image_pdfimage_print
   
 

//http://bbinjest.codeplex.com/
//Apache License 2.0 (Apache)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace BneyBaruch.Ingest.Utils.FileSystem
{
    public static class DriveUtil
    {


        /// <summary>
        ///     Check drive free space.
        /// </summary>
        /// <param name="driveName">Drive name to check free space.</param>
        /// <param name="minimumSpace">Minimum space in bytes.</param>
        /// <returns></returns>
        public static bool IsEnoughSpace(string driveName, long minimumSpace)
        {
            if (string.IsNullOrEmpty(driveName))
                throw new ArgumentNullException("driveName");

            string formattedDriveName = driveName + @":";

            DriveInfo[] drives = DriveInfo.GetDrives();

            DriveInfo drive = drives.FirstOrDefault(
                delegate(DriveInfo innerDrive)
                {
                    return string.Compare(innerDrive.Name, formattedDriveName, true) == 0;
                }
            );

            if (drive == null)
                throw new DriveNotFoundException(string.Format("Drive {0} could not be found.", formattedDriveName));

            if( !drive.IsReady)
                throw new DriveNotFoundException(string.Format("Drive {0} is not ready.", formattedDriveName));

            return drive.TotalFreeSpace > minimumSpace;
        }
   }
}

   
     


This entry was posted in File Stream. Bookmark the permalink.