How to list logical drives without mapped drives

I would like to populate a list of logical drives, but I would like to exclude any mapped drives. The following code gives me a list of all logical drives without filtering.

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

Is there a way that can help you identify between physical disks and mapped disks?

+4
source share
7 answers

You can 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 drive when DriveType property is Network

+4
source

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

+2
source

you can use the DriveType property in the DriveInfo class

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

The first thing that comes to mind is that the mapped drives will have a line starting with \\

Here, another more extensive but more reliable approach is described in detail: How to programmatically detect mapped network drives in the system and their server names?


Or try calling DriveInfo.GetDrives() , which will give you objects with lots of metadata to help you filter afterwards. Here is an example:

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

0
source

Try using System.IO.DriveInfo.GetDrives :

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

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

(This is a VB project.)

0
source

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

All Articles