C# - How to Get Computer Drive Names of Given Directory?

Learn, how to get computer drive names using C# program?
Submitted by IncludeHelp, on November 13, 2017 [Last updated : March 26, 2023]

Get Computer Drive Names of Given Directory

To get computer drive names of given directory in C#, we use Directory.GetLogicalDrives() method.

Directory.GetLogicalDrives()

This is a method of 'Directory' class, it returns the local drive (computer drive) name.

Syntax

string[] Directory.GetLogicalDrives();

Parameter(s)

  1. None

Return Value

This method returns the array of string that contains computer drive names.

C# program to get computer drive names of given directory

using System;
using System.IO;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      string[] drives;

      drives = Directory.GetLogicalDrives();

      Console.WriteLine("Computer Drives:");
      for (int i = 0; i < drives.Length; i++) {
        Console.WriteLine("\t" + drives[i]);
      }

    }
  }
}

Output

Computer Drives:
        C:\
        D:\
        E:\
        F:\

Explanation

In the above program, we need to remember, when we use "Directory" class, System.IO namespace must be included in the program.

C#.Net Directory Class Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.