DESKTOP

Enumerate Drives

9/25/2010 3:16:17 PM
Problem : You want to get a list of all drives installed on the system.

Solution:
The DriveInfo class provides static methods to retrieve this information.

DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo info in drives)
{
Console.WriteLine("Name: {0} (RootDirectory: {1}",
info.Name, info.RootDirectory);
Console.WriteLine("DriveType: {0}", info.DriveType);
Console.WriteLine("IsReady: {0}", info.IsReady);
//you'll get an exception if you try to read some
//info when the drive isn't ready
if (info.IsReady)
{
Console.WriteLine("VolumeLabel: {0}", info.VolumeLabel);

Console.WriteLine("DriveFormat: {0}", info.DriveFormat);
Console.WriteLine("TotalSize: {0:N0}", info.TotalSize);
Console.WriteLine("TotalFreeSpace: {0:N0}", info.TotalFreeSpace);
Console.WriteLine("AvailableFreeSpace: {0:N0}",
info.AvailableFreeSpace);
}

Console.WriteLine();
}

This code produces the following output on my system (edited to show only unique drives):

Name: A:\ (RootDirectory: A:\)
DriveType: Removable
IsReady: False

Name: C:\ (RootDirectory: C:\)
DriveType: Fixed
IsReady: True
VolumeLabel:
DriveFormat: NTFS
TotalSize: 160,045,199,360
TotalFreeSpace: 81,141,878,784
AvailableFreeSpace: 81,141,878,784

Name: F:\ (RootDirectory: F:\)
DriveType: CDRom
IsReady: False

Name: H:\ (RootDirectory: H:\)
DriveType: Removable
IsReady: False

Other  
  •  Get File Security Description
  •  Combine Streams (Compress a File)
  •  Create, Read, and Write a Binary File
  •  Create, Read, and Write a Text File
  •  Opening a Local File from a Silverlight Application
  •  Navigating Architecture Changes in Windows Vista
  •  Getting Around Windows Vista
  •  Managing User Account Control and Elevation Prompts
  •  Supporting Computers Running Windows Vista
  •  Using System Support Tools in Vista
  •  Managing System Properties
  •  Introducing Automated Help And Support in Vista
  •  Working with the Automated Help System
  •  Installing and Maintaining Devices in Vista: The Essentials
  •  Getting Started with Device Manager
  •  Working with Device in Vista
  •  Managing Hardware in Vista
  •  Customizing Hardware Device Settings
  •  Managing Internet Time in Vista
  •  Optimizing Windows Vista Menus
  •  
    Most View
    Get Your iPhotos Organised
    Parallel Programming with Microsoft .Net : Pipelines - Variations
    Programming Microsoft SQL Server 2005: Using Data Mining Extensions (part 2) - Data Mining Predictions Using DMX
    LINQ to Objects : How to Sort the Results
    Communicating with the Silverlight Plug-in
    Windows Vista : Communicating with Windows Mail - Filtering Incoming Messages
    Nikon Coolpix S3300
    Network Attached Storage Round-Up (Part 1) - The Benefits Of A NAS
    Visual Studio 2010 : Structured Exception Handling to the Rescue
    The drive toward DSLs : Taking a DSL apart—what makes it tick?
    Top 10
    Buying Guide: All-In-One Multifunction Printers – May 2013 (Part 3)
    Buying Guide: All-In-One Multifunction Printers – May 2013 (Part 2)
    Buying Guide: All-In-One Multifunction Printers – May 2013 (Part 1)
    Arduino Due - A Microcontroller Board
    Compact Liquid Cooling Systems Roundup – Front Runners (Part 4)
    Compact Liquid Cooling Systems Roundup – Front Runners (Part 3)
    Compact Liquid Cooling System Roundup – Front Runners (Part 2)
    Compact Liquid Cooling System Roundup – Front Runners (Part 1)
    The Best PC Deals Around – May 2013 (Part 2)
    The Best PC Deals Around – May 2013 (Part 1)