5

I would like to populate a combo box with a list of logical drives but I would like to exclude any mapped drives. The code below gives me a list of all logical drives without any filtering.

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

Is there a method available that can help you determine between physical drives and mapped drives?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
robmo
  • 61
  • 1
  • 5

7 Answers7

5

You could use the DriveInfo class

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

exclude the drive when the property DriveType is Network

Steve
  • 213,761
  • 22
  • 232
  • 286
2

Use DriveInfo.GetDrives to get the list of drives. You can then filter the list by its DriveType property.

Timbo
  • 27,472
  • 11
  • 50
  • 75
2

you can use DriveType property in the DriveInfo class

 DriveInfo[] dis = DriveInfo.GetDrives();
 foreach ( DriveInfo di in dis )
 {
     if ( di.DriveType == DriveType.Network )
     {
        //network drive
     }
  }
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
0

One first thing that comes to mind is that the mapped drives will have the string beginning with \\

Another approach which is more extensive but more reliable is detailed here: How to programmatically discover mapped network drives on system and their server names?


Or try calling DriveInfo.GetDrives() which will give you objects with more metadata that will help you filter afterwards. Here's an example:

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

Community
  • 1
  • 1
dutzu
  • 3,883
  • 13
  • 19
0

Try using System.IO.DriveInfo.GetDrives:

comboBox.Items.AddRange(
       System.IO.DriveInfo.GetDrives()
                          .Where(di=>di.DriveType != DriveType.Network)
                          .Select(di=>di.Name));
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

The most complete information I found on this subject (after a long search on the internet) is available on Code Project: Get a list of physical disks and the partitions on them in VB.NET the easy way

(It's a VB project.)

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

This is what worked for me:

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

}
robmo
  • 61
  • 1
  • 5