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, the 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();

DriverInfo1.jpg


DriveInfo Class:

The System.IO namespace has a class which helps the users to interact with the system to get the file details, directory details and system information. The DriveInfo class is a readonly 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.

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

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

Name:

The name is one of the property in the DriveInfo class which gives the name of the drive like C or D...

DriverInfo2.jpg

DriveType:

The DriveType is enum type in the System.IO namespace and it has set of enum numbers.

         DriveType.jpg

It will give the details of type f DriveType. As you know that there are some times of drive like system drive, removable drive, cd rom...

Ready:

This is Boolean type of property which tell 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 with the device.

TotalSize:

The total size of the drive can be manipulated based on the device state. If it is system drive then it will be ready when the OS is running. But 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 peoperty 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 in the article.

 

Up Next
    Ebook Download
    View all
    Learn
    View all