Drives Handling

• .NET support to access the available drives information on the computer such as “C: drive”, “D: drive” etc.
• This drives may include with hard disks, CD-ROM drives, DVD-ROM drives, pen drives etc.
• It offers to get the volume labels, total space and current free space of the drives etc.

API: System.IO.DriveInfo

The “DriveInfo” class object can represent a drive on the computer. It offers several properties and methods to access the related information of the drive.

Object Construction:
Syn: DriveInfo obj = new DriveInfo(“drive letter”);
Ex: DriveInfo obj = new DriveInfo(“c”);
You can observe the list of all available properties, methods of this class.
Properties of “DriveInfo” class
IsReady Indicates whether the drive is ready or not (true / false)
Name Represents the name of the drive with drive letter. (Ex: c:\)
DriveType Represents the type of the drive. (like Fixed, CD Rom, Removable)
VolumeLabel Represents the volume label
DriveFormat Represents the file system on the drive like FAT32, NTFS, CDFS etc.
TotalSize Represents the total memory capacity of the drive in bytes.
GetDrives() Returns all the currently available drive objects in the form of an array.

Demo on “DriveInfo” class – For a Single Drive

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DriveManipulationsDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the drive letter (a to z):");
string driveletter = Console.ReadLine();
DriveInfo d = new DriveInfo(driveletter);
if (d.IsReady)
Console.WriteLine(d.Name + " - " + d.DriveType + " - " + d.VolumeLabel + " - " + d.DriveFormat + " - " + d.TotalSize + " bytes - " + d.TotalFreeSpace + " bytes.");
else
Console.WriteLine(d.Name + " - " + " Not Ready.");
Console.Read();
}
}
}
drive handling

Demo on “DriveInfo” class – For Multiple Drives

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GetDrivesDemo
{
class Program
{
static void Main(string[] args)
{
DriveInfo[] dinfo = DriveInfo.GetDrives();
Console.WriteLine(dinfo.Length + " drives found on this computer.\n");
foreach (DriveInfo d in dinfo)
{
if (d.IsReady)
Console.WriteLine(d.Name + " - " + d.DriveType + " - " + d.VolumeLabel + " - " + d.DriveFormat + " - " + d.TotalSize + " bytes - " + d.TotalFreeSpace + " bytes.");
else
Console.WriteLine(d.Name + " - " + d.DriveType + " - Not Ready.");
}
Console.Read();
}
}
}
}
drive handling