List all drives in CSharp

Description

The following code shows how to list all drives.

Example


  // w ww. j  av a  2s . c  o m

using System;
using System.IO;

static class MainClass
{
    static void Main(string[] args)
    {
        foreach (DriveInfo drive in DriveInfo.GetDrives())
        {
            try
            {
                Console.WriteLine(
                    "{0} - {1} KB",
                    drive.RootDirectory,
                    drive.AvailableFreeSpace / 1024
                    );
            } catch (IOException) {
                Console.WriteLine(drive);
            }
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip