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

时间:2013-01-31 16:53:26

标签: c#

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

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

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

7 个答案:

答案 0 :(得分:4)

您可以使用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

时排除驱动器

答案 1 :(得分:2)

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

答案 2 :(得分:2)

您可以在DriveType

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

答案 3 :(得分:0)

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

此处详细介绍了另一种更广泛但更可靠的方法:How to programmatically discover mapped network drives on system and their server names?


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

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

答案 4 :(得分:0)

尝试使用System.IO.DriveInfo.GetDrives

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

答案 5 :(得分:0)

我在代码项目中提供了有关此主题的最完整信息(经过长时间的互联网搜索后):Get a list of physical disks and the partitions on them in VB.NET the easy way

(这是一个VB项目。)

答案 6 :(得分:0)

这对我有用:

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);
    }

}