C# Environment GetLogicalDrives

.NET Framework information

What disk drives are present on the current computer? With the Environment.GetLogicalDrives method and the C# language, you can determine where to search for files on local drives.

Example

To start, the Environment.GetLogicalDrives method returns a string array. Logical drives may not be separate physically, but to the Windows operating system, they are "logically" separate. Thus, a partition on a hard disk is a logical drive. A USB disk is also a logical as well as physical drive.

String Array

This C# example demonstrates the Environment.GetLogicalDrives method.

Program that shows GetLogicalDrives [C#]

using System;

class Program
{
    static void Main()
    {
	string[] value = Environment.GetLogicalDrives();
	Console.WriteLine(string.Join(",", value));
    }
}

Output

C:\,D:\,H:\

Detection

Question and answer

How can you detect when a user inserts (mounts) a drive on the current computer? One way you could do this is simply call GetLogicalDrives on a Timer. Ask the computer what drives are mounted every several seconds, and then if this array changes, you know something was added or removed.

Timer Tutorial

Summary

Here, we discussed the concept of logical drives and the usage of the GetLogicalDrives method in the C# language. This useful method will help you enumerate the available file systems on the local computer, without knowing the drive letter in advance.

.NET Framework Info
.NET