Check drive free space. : Drive « File Stream « C# / C Sharp






Check drive free space.

  

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

   
    
  








Related examples in the same category

1.DriveInfo(file.FullName) Name
2.Is Drive Ready
3.Drive Format
4.Available Free Space
5.Drive Type
6.Root Directory
7.Provides access to information on a drive.