Mega Code Archive

 
Categories / C# Book / 07 Stream
 

0578 Querying Volume Information

You can query the drives on a computer with the DriveInfo class: using System; using System.IO; using System.Linq; using System.Text; class Program { static void Main() { DriveInfo c = new DriveInfo("C"); // Query the C: drive. long totalSize = c.TotalSize; // Size in bytes. long freeBytes = c.TotalFreeSpace; // Ignores disk quotas. long freeToMe = c.AvailableFreeSpace; // Takes quotas into account. foreach (DriveInfo d in DriveInfo.GetDrives()) // All defined drives. { Console.WriteLine(d.Name); // C:\ Console.WriteLine(d.DriveType); // Fixed Console.WriteLine(d.RootDirectory); // C:\ if (d.IsReady) { Console.WriteLine(d.VolumeLabel); Console.WriteLine(d.DriveFormat); } } } } The output: C:\ Fixed C:\ NTFS D:\ CDRom ... ... The static GetDrives method returns all mapped drives, including CD-ROMs, media cards, and network connections. DriveType is an enum with the following values: CDRom Fixed Network NoRootDirectory Ram Removable Unknown