Get all drives in C#

CsharpProgrammingServer Side Programming

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:\
raja
Published on 05-Sep-2018 10:53:02
Advertisements