CHAPTER 20 FILE I/O AND OBJECT SERIALIZATION
type, available free space, and volume label). Consider the following Program class defined within a new
Console Application named DriveInfoApp (don’t forget to import System.IO):
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with DriveInfo *****\n");
// Get info regarding all drives.
DriveInfo[] myDrives = DriveInfo.GetDrives();
// Now print drive stats.
foreach(DriveInfo d in myDrives)
{
Console.WriteLine("Name: {0}", d.Name);
Console.WriteLine("Type: {0}", d.DriveType);
// Check to see whether the drive is mounted.
if(d.IsReady)
{
Console.WriteLine("Free space: {0}", d.TotalFreeSpace);
Console.WriteLine("Format: {0}", d.DriveFormat);
Console.WriteLine("Label: {0}", d.VolumeLabel);
}
Console.WriteLine();
}
}
Console.ReadLine();
}
Here is some possible output:
***** Fun with DriveInfo *****
Name: C:\
Type: Fixed
Free space: 587376394240
Format: NTFS
Label: Mongo Drive
Name: D:\
Type: CDRom
Name: E:\
Type: CDRom
Name: F:\
Type: CDRom
Name: H:\
Type: Fixed
Free space: 477467508736
761