Skip to content
Related Articles

Related Articles

C# Program to Get the Logical Drives of Computer System Using Environment Class

Improve Article
Save Article
Like Article
  • Last Updated : 01 Nov, 2021

In C#, Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that it retrieves command-line arguments information, exit codes information, environment variable settings information, contents of the call stack information, and time since the last system boot in milliseconds information. In this article, we will get the logical drives of the computer system. So to solve this problem we use the GetLogicalDrives() method of the Environment Class. This method is used to get the logical drives present in your computer. It will return the array of drives in the string format. 

Syntax:

string[] Environment.GetLogicalDrives()

Return: The return type of this method is an array of strings. And this array of strings holds the name of the logical drives present in the computer system.

Exception: This method also throws the following exceptions:

  • IOException: GetLogicalDrives() method throws this exception when the Input/Output error occurs.
  • SecurityException: GetLogicalDrives() method throws this exception when the caller does not have suitable permission.

Example:

C#




// C# program to find the logical drives of 
// current computer system. Using the
// Environment class
using System;
  
class GFG{
      
static public void Main()
{
      
    // Declare the string array to 
    // store the logical drives
    string[] drives = Environment.GetLogicalDrives();
      
    Console.WriteLine("Logical Drives of this computer:");
      
    // Display all drives present in the computer system
    foreach (string d in drives)
    {
        Console.WriteLine("\t" + d);
    }
}
}

Output:

Logical Drives of this computer:
C:\
D:\
E:\
F:\
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!