Environment.GetLocalDrives() Method


If you want to get all the drives that are existing in your PC using C#, the Environment class supports the GetLogicalDrives() method.

First, we should see what an Environment class is.

The Environment class in C# provides an information about the working environment like variables, methods used, and system related information.

Let’s discuss the GetLogicalDrives() method in detail with their respective examples.

Environment.GetLogicalDrives()

The GetLogicalDrives() method in C# returns the drives that are present in our system. It takes a string array to store the drives. We can use a foreach loop to display each drive one by one.

Syntax:

String[] logical_drives_exists = Environment.GetLogicalDrives();

Return Type:

It returns the array of strings.

Exceptions:

  1. IO Exception – If the drives are not present in our system, it returns Input/Output Exception.
  2. SecurityException – Suppose there are any security permissions and the user is not able to access respect drive, the SecurityException occurs.

Example 1:

Let’s display the drives present in the system.

using System;
class Linuxhint
{
    //let‘s implement the GetLogicalDrives property inside main method
    static public  void Main()
    {
        //store the logical drives in an string array
        String[] logical_drives_exists = Environment.GetLogicalDrives();
       
        Console.WriteLine(«Drives:»);
       
        //return all drives one by one
         foreach (string each_drive in logical_drives_exists){
        Console.WriteLine(each_drive);
               }

    }
}

Output:

Example 2:

It is also possible to get all the drives at a time from an array of strings using the String.Join() function.

Syntax:

String.Join(«,», logical_drives_exists)

using System;
class Linuxhint
{
    //let‘s implement the GetLogicalDrives property inside main method
    static public  void Main()
    {
        //store the logical drives in an string array
        String[] logical_drives_exists = Environment.GetLogicalDrives();
       
       
        //return all drives at a time separated by comma
        Console.WriteLine(«Drives:»+ String.Join(«,», logical_drives_exists));
   
   
    }
}

Output:

Explanation:

Line 5:

We implement our property inside the Main method.

Line 8:

Create a variable that is an array of strings used to store the drives.

String[] logical_drives_exists = Environment.GetLogicalDrives();

Line 11:

Use the String.Join() function to return all the drives.

Console.WriteLine(«Drives:»+ String.Join(«,», logical_drives_exists));

Conclusion

In this C# tutorial, we learned how to return the drives that are present in our system using the GetLogicalDrives() method . It takes an array of strings to store the drives. We discussed two examples to get the drives one by one using a for loop and get the drives at a time using the String.Join() method. If there are any security permissions and the user is not able to access the drive, the SecurityException occurs.



Source link