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.
  1. DriveInfo[] driveInfo = DriveInfo.GetDrives(); 
DriverInfo1.jpg
 

DriveInfo Class

 
The System.IO namespace has a class that helps the users to 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 the one by one.
  1. DriveInfo driveInfo = new DriverInfo("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...
 
DriverInfo2.jpg
 
DriveType:
 
The DriveType is an enum type in the System.IO namespace and it has a set of enum numbers.
         
DriveType.jpg
 
It will give the details of the type f DriveType. As you know that there are some times of drive like system drive, removable drive, cd rom...
 
Ready:
 
This is a Boolean type of property that tells us whether the drive is ready or not. Because some the drive 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 state. If it is a system drive then it will be ready when the OS is running. But at the same time, the device 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 and which 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.
 
DriverInfo3.jpg
 

Conclusion

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