如何列出没有映射驱动器的逻辑驱动器

我想用一个逻辑驱动器列表填充一个combobox,但我想排除任何映射的驱动器。 下面的代码为我提供了所有逻辑驱动器的列表,没有任何过滤。

comboBox.Items.AddRange(Environment.GetLogicalDrives()); 

是否有可用的方法可以帮助您确定物理驱动器和映射驱动器?

您可以使用DriveInfo类

  DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { Console.WriteLine("Drive {0}", d.Name); Console.WriteLine(" File type: {0}", d.DriveType); if(d.DriveType != DriveType.Network) { comboBox.Items.Add(d.Name); } } 

属性DriveTypeNetwork时排除驱动器

使用DriveInfo.GetDrives获取驱动器列表。 然后,您可以按其DriveType属性筛选列表。

您可以在DriveInfo类中使用DriveType属性

  DriveInfo[] dis = DriveInfo.GetDrives(); foreach ( DriveInfo di in dis ) { if ( di.DriveType == DriveType.Network ) { //network drive } } 

首先想到的是映射的驱动器将以\\开头的字符串

这里详细介绍了另一种更广泛但更可靠的方法: 如何以编程方式发现系统上的映射网络驱动器及其服务器名称?


或者尝试调用DriveInfo.GetDrives() ,它将为您提供更多元数据,以帮助您在之后进行过滤。 这是一个例子:

http://www.daniweb.com/software-development/csharp/threads/159290/getting-mapped-drives-list

尝试使用System.IO.DriveInfo.GetDrives

 comboBox.Items.AddRange( System.IO.DriveInfo.GetDrives() .Where(di=>di.DriveType != DriveType.Network) .Select(di=>di.Name)); 

我在代码项目上可以获得有关此主题的最完整信息(在互联网上进行长时间搜索之后): 在VB.NET中获取物理磁盘及其分区列表的简单方法

(这是一个VB项目。)

这对我有用:

 DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { if (d.IsReady && (d.DriveType == DriveType.Fixed || d.DriveType == DriveType.Removable)) { cboSrcDrive.Items.Add(d.Name); cboTgtDrive.Items.Add(d.Name); } }