×

C# Tutorial

Basics of .Net

C# Basics

C# Fundamentals

C# Operators

C# Loops

C# Type Conversions

C# Exception Handling

C# String Class

C# Arrays

C# List

C# Stack

C# Queue

C# Collections

C# Character Class

C# Class and Object

C# Namespaces

C# Delegates

C# Constructors

C# Inheritance

C# Operator Overloading

C# Structures

C# File Handling

C# Convert.ToInt32()

C# Int32 (int) Struct

C# DateTime Class

C# Uri Class

C# Database Connectivity

C# Windows

C# Other Topics

C# Q & A

C# Programs

C# Find O/P

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 »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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