首先,使用GetDrives获取所有驱动器的名称-
var drv = DriveInfo.GetDrives();
循环遍历以获取系统上所有驱动器的名称-
foreach (DriveInfo dInfo in drv) {
Console.WriteLine(dInfo.Name);
}让我们看完整的代码-
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);
}
}
}输出结果
/etc/resolv.conf /etc/hostname /etc/hosts /run/secrets /home/cg/root
注意:结果在不同的操作系统上会有所不同。以上输出显示在Linux OS上。
在Windows OS上将产生以下结果:
C:\ D:\ E:\