Get all drives in C#


Firstly, use GetDrives to get the name of all the drives −

var drv = DriveInfo.GetDrives();

Loop through to get the name of all the drives on the system −

foreach (DriveInfo dInfo in drv) {
   Console.WriteLine(dInfo.Name);
}

Let us see the complete code −

Example

 Live Demo

using System;
using System.Linq;
using System.IO;
public class Demo {
   public static void Main() {
      var drv = DriveInfo.GetDrives();
      foreach (DriveInfo dInfo in drv) {
         Console.WriteLine(dInfo.Name);
      }
   }
}

Output

/etc/resolv.conf
/etc/hostname
/etc/hosts
/run/secrets
/home/cg/root

Note: The result will vary on different Operating Systems. The above output is shown on Linux OS.

It will produce the following result on Windows OS:

C:\
D:\
E:\

Updated on: 22-Jun-2020

545 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements