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
  •  
    Top 10
    Mobile Application Security : The Apple iPhone - Push Notifications, Copy/Paste, and Other IPC
    Exploring the T-SQL Enhancements in SQL Server 2005 : The WAITFOR Command
    Parallel Programming with Microsoft .Net : Parallel Aggregation - Variations
    Optimizing an Exchange Server 2010 Environment : Analyzing Capacity and Performance
    Programming .NET Security : Hashing Algorithms Explained
    Sharepoint 2007: Specify Your Colleagues
    Algorithms for Compiler Design: THE NFA WITH ∈-MOVES
    Choosing The Right Parts For Your Build (Part 1) - Picking the perfect processor
    Choosing The Right Parts For Your Build (Part 5) - Choosing your case & Picking the right storage
    SQL Server 2008 : Leveraging the Microsoft Sync Framework
    Most View
    Legal Trouble with Social Networks (Part 1)
    The choices of mobile computing for SOHO users (part 2)
    Infrastructure Security: The Application Level
    Sharepoint 2007: Create a New List Item
    SQL Azure Data Access
    Getting Started with MySQL Enterprise & MySQL Enterprise Components
    iPhone Application Development : Using Advanced Interface Objects and Views - User Input and Output
    How to Protect Your Mobile Devices
    Joomla! Blogging and RSS Feeds : Commenting anyone?
    The Second BlackBerry Developers Conference Asia (Part 2)
    Windows Azure : Understanding the Blob Service
    Windows Server 2008 : Understanding the Identity Management for UNIX Components
    Migrating from Legacy SharePoint to SharePoint Server 2010 : Using Visual Upgrade
    Designing and Implementing Mobility in Exchange Server 2010 : Securing Access to ActiveSync Using Internet Security and Acceleration (ISA) Server 2006
    SQL Server 2008 : Explaining Advanced Query Techniques - Creating CTEs
    Configuring Server Roles in Windows 2008 : New Roles in 2008
    Mass Effect Infiltrator
    iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 1) - A Super Simple Sample App for Supersampling
    Changes in Windows Vista Affecting SDI
    Search for a File or Directory