DriveInfo Class in C#

Developers and programmers have to handle with file system in their normal programming practice. Which includes accessing drives, folders and files for operation. System.IO namespace in .NET Framework provides many classes to work with the file system. One of the classes is DriveInfo class.

DriveInfo class is used to get information about computers system. This class models a drive and provides methods and properties to query for drive information.

Use DriveInfo to determine what drives are available, and what type of drives they are. You can also query to determine the capacity and available free space on the drive.

The following table describes some commonly used methods of DriveInfo class:


Name

Description

Equals

Determines whether the specified Object is equal to the current Object. (Inherited from Object.)

Finalize

Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)

GetDrives

Retrieves the drive names of all logical drives on a computer.

GetHashCode

Serves as a hash function for a particular type. (Inherited from Object.)

GetType

Gets the Type of the current instance. (Inherited from Object.)

MemberwiseClone

Creates a shallow copy of the current Object. (Inherited from Object.)

ToString

Returns a drive name as a string. (Overrides Object.ToString().)

The following table describes some commonly used properties of DriveInfo class:

Name

Description

AvailableFreeSpace

Indicates the amount of available free space

DriveFormat

Gets the name of the file system, such as NTFS or

DriveType

Gets the drive type.

IsReady

Gets a value indicating whether a drive is ready.

Name

Gets the name of a drive.

RootDirectory

Gets the root directory of a drive.

TotalFreeSpace

Gets the total amount of free space available

TotalSize

Gets the total size of storage space on a drive.

VolumeLabel

Gets or sets the volume label of a drive.

The following code example demonstrates the use of the DriveInfo class to display information about all of the drives on the current system.

using
System;
using System.IO;
namespace DriveInfo_example
{
    class Program
    {
        static void Main(string[] args)
        {
            // GetDrives() method retrieves the drive names of all logical drives on a computer. 
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                if (drive.IsReady)
                {
                    Console.WriteLine("Drive Name : " + drive.Name);
                    Console.WriteLine("Drive Volume name : " + drive.VolumeLabel);
                    Console.WriteLine("Drive Format : " + drive.DriveFormat);
                    Console.WriteLine("Drive Type : " + drive.DriveType);
                    Console.WriteLine("Drive root directory name : " + drive.RootDirectory);
                    Console.WriteLine("Drive free space: " + drive.AvailableFreeSpace);
                    Console.WriteLine("Total Free space on the drive : " + drive.TotalFreeSpace);
                    Console.WriteLine("Total disk size : " + drive.TotalSize);
                    Console.WriteLine();
                }
            }
            Console.ReadLine();
        }
    }
}

Note:
In the DriveInfo classes there are two very similar properties, AvailableFreeSpace and TotalFreeSpace. Most of the cases you will find same number output.

AvailableFreeSpace property indicates the amount of free space available on the drive. While the TotalFreeSpace number because this property takes into account disk quotas.

What is Disk Quotas?

To monitoring the disk space used on a system, disk space can be restricted by implementing disk quotas so that the system administrator is alerted before a user consumes too much disk space or a partition becomes full.

Disk quotas can be configured for individual users as well as user groups. This kind of flexibility makes it possible to give each user a small quota to handle file, while allowing the projects they work on to have more sizable quotas.

To view disk quota information using the windows interface
 

  1. Open My Computer.

  2. Right-click the volume for which you want to view quota information, and then click Properties.

  3. In the Properties dialog box, click the Quota tab.

  4. On the Quota tab, click Quota Entries.

Conclusion

I hope that this article would have helped you in understanding DriveInfo Class. Your feedback and constructive contributions are welcome.


Similar Articles