DriveInfo Class in C# with an Example

Introduction

The DriveInfo class in C# is one of the important classes in the System.IO namespace since it can get the system information like drive type, space, etc.., We will discuss this class in detail with a sample application.

How to Get the Drives?

The drives in the system can be used by the GetDrives method in the DriveInfo class. It will return a collection of DriveInfo objects.

DriveInfo[] driveInfo = DriveInfo.GetDrives();

Drives

DriveInfo Class

The System.IO namespace has a class that helps the users interact with the system to get the file details, directory details, and system information. The DriveInfo class is a read-only class and can be used to get details about the drives.

It has the following properties in the DriveInfo class. Let us see them one by one.

DriveInfo driveInfo = new DriveInfo("Drive Name");

The drive name has to be passed as a constructor to get the details about the drive in the system.

  • Name: The name is one of the properties in the DriveInfo class which gives the name of the drive like C or D.
     DriveInfo class
  • DriveType: The DriveType is an enum type in the System.IO namespace and it has a set of enum numbers.
    DriveType
  • It will give the details of the type f DriveType. As you know there are some times of drives like system drives, removable drives, cd rom.
  • Ready: This is a Boolean type of property that tells us whether the drive is ready or not. Because some of the drives allocated for the device like removable, CD ROM. But it will be enabled when the device is plugged into the device.
  • TotalSize: The total size of the drive can be manipulated based on the device's state. If it is a system drive then it will be ready when the OS is running. But at the same time, devices like Removable and CD ROW size will be manipulated only when the state is Ready. Otherwise, it will not give any such details.
  • AvailableFreeSpace: This is also one of the properties in the class that tells the available space in the drive when the state of the device is ready.
  • VolumeLabel: Every drive will have the volume label, which will be returned by the driveinfo class.
    Volume label

Conclusion

The DriveInfo class is one of the very useful classes to scale the drive information. The source code is attached to the article.