C# Directory GetLogicalDrives() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, Directory.GetLogicalDrives() returns the drive roots of the computer as a string[]. On Windows, you get items such as C:\ and D:\. On Linux and macOS, you get the root path /.

The list shows what is on this machine, so no two computers give the same answer. Use DriveInfo.GetDrives() when you also need the size, free space, or type of each drive.

This is a static method of System.IO.Directory. Call it as Directory.GetLogicalDrives(...); there is no new Directory(). For an object that remembers one folder, see DirectoryInfo.

Syntax

The method takes no arguments. Loop over the array or use its Length.

C#
string[] drives = Directory.GetLogicalDrives();

Console.WriteLine(drives.Length > 0);   // True on a normal computer
// Each item is a root such as "C:\" or "/" (varies by machine)
CallWhat it does
Directory.GetLogicalDrives()Returns the roots of the drives as a string[].
DriveInfo.GetDrives()Returns DriveInfo objects with size, free space, and type.
Path.GetPathRoot(path)Gets the root of one path, so you can compare it with the list.

Do not print the list in a tutorial or a test, since it is different on each computer and may even change while the program runs (a USB stick is plugged in). The examples print counts and true or false facts instead.

Library declarations
C#
public static string[] GetLogicalDrives();

Parameters

ParameterDescription
NoneGetLogicalDrives() takes no parameters.

Return Value

Return ValueDescription
string[]An array of drive root paths. Each starts with a drive or a slash and ends with a path separator.

C# Examples

Example 1 — Check the List Without Printing It

The drive list differs on every computer, so test its shape rather than its content.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string[] drives = Directory.GetLogicalDrives();

        Console.WriteLine("Has drives: " + (drives.Length > 0));

        bool allRooted = true;
        foreach (string drive in drives) {
            if (!Path.IsPathRooted(drive)) allRooted = false;
        }
        Console.WriteLine("All rooted: " + allRooted);
    }
}

How It Works

  1. At least one drive. Every normal computer has a root, so Length > 0 is True. How many more there are depends on the machine.
  2. Start true, then try to disprove. The flag begins as true and turns false if any drive fails the test. This is the usual way to check a whole array.
  3. Each item is rooted. A drive root such as C:\ or / starts at the top, so Path.IsPathRooted accepts all of them.

Example 2 — Find Out Whether a Drive Can Be Read

Use Directory.Exists to skip drives that have no media.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string[] drives = Directory.GetLogicalDrives();

        int total = drives.Length;
        int ready = 0;
        foreach (string drive in drives) {
            if (Directory.Exists(drive)) ready++;
        }

        Console.WriteLine("Listed at least one: " + (total >= 1));
        Console.WriteLine("Ready at least one: " + (ready >= 1));
        Console.WriteLine("Ready is not more than listed: " + (ready <= total));
    }
}

How It Works

  1. Listed is not ready. A CD drive with no disk is in the array, so the code counts only the drives where Directory.Exists is True.
  2. Counts, not names. The totals differ between computers, so the program prints comparisons that are the same everywhere.
  3. The system drive is ready. The drive that holds the program always works, so ready is at least 1.
  4. A basic rule. The ready count can never be bigger than the listed count, and the last line confirms it.

Example 3 — Sort a Copy and Check the Format

Sort the drive names in a new array and check how each one ends.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string[] drives = Directory.GetLogicalDrives();
        string[] sorted = (string[])drives.Clone();
        Array.Sort(sorted, StringComparer.Ordinal);

        bool inOrder = true;
        for (int i = 1; i < sorted.Length; i++) {
            if (string.CompareOrdinal(sorted[i - 1], sorted[i]) > 0) inOrder = false;
        }

        string sep = Path.DirectorySeparatorChar.ToString();
        bool endsWithSeparator = true;
        foreach (string drive in drives) {
            if (!drive.EndsWith(sep)) endsWithSeparator = false;
        }

        Console.WriteLine("Same size: " + (sorted.Length == drives.Length));
        Console.WriteLine("Sorted: " + inOrder);
        Console.WriteLine("Ends with separator: " + endsWithSeparator);
    }
}

How It Works

  1. Do not sort the original. Clone() makes a copy, so drives keeps the order that the system gave.
  2. Same count. Sorting moves items around and never adds or removes any, so both arrays have the same Length.
  3. Check neighbours. The loop compares each item with the one before it, and a single out-of-order pair would set inOrder to false.
  4. Ends like a folder. C:\ ends with \ and / ends with /, so Path.DirectorySeparatorChar matches on every operating system.

Frequently Asked Questions

A string[] of the drive roots on the computer, such as C:\ and D:\ on Windows, or / on Linux and macOS.
GetLogicalDrives only returns the root strings. DriveInfo.GetDrives returns objects that also give the drive type, label, size, and free space.
Because each computer has different drives. Print counts or true and false checks in shared code instead of the actual list.
A drive such as an empty card reader is listed, but no media is inside, so it cannot be read. Check the ready state before you use it.
Not by promise. Copy the array and call Array.Sort on the copy when you need a stable order.

Next: GetParent()

Get the parent folder of a path as a DirectoryInfo; null for a root.

GetParent() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful