Recover network-mapped disk drive volume name

3

I'm using DriveInfo.GetDrivers() to get the names of the disk drives present on the machine and list them in TreeView .

I've made the code below that works, but the network disk names that my machine has access to do not appear, only the local disks.

foreach(DriveInfo drv in DriveInfo.GetDrives())
{
    TreeNode node = new TreeNode();
    node.ImageIndex = 0;
    node.SelectedImageIndex = 0;
    node.Text = drv.Name+drv.VolumeLabel; 
    node.Nodes.Add("");
    treeview.Nodes.Add(node);
    retorno = true;
}

When I concatenate with drv.VolumeLabel that the network disks stop appearing (concatenate to appear to the user the name of the disk for identification)

I'm thinking of a if to display them, but the purpose was to display the name of the networked disk, since much of the work will be on different networks.

Update:

I made a If to properly display the disk name and display at least the network dr

if (drv.DriveType == DriveType.Network)
   node.Text = drv.Name;
else if (drv.DriveType == DriveType.Fixed)
   node.Text = drv.Name + drv.VolumeLabel;
else
   node.Text = drv.Name;

and is displayed like this:

The problem is that in this environment there is pendrive on, network HD and CD-ROM disk in the middle.

    
asked by anonymous 21.03.2016 / 17:50

1 answer

3

Apparently the way to do this is with WMI, according to this response in the SO . Take the test with this code to see if it returns what you want and adapt to what you need.

    var searcher = new ManagementObjectSearcher("root\CIMV2",
        "SELECT * FROM Win32_MappedLogicalDisk"); 
    foreach (var queryObj in searcher.Get()) {
        Console.WriteLine("VolumeName: {0}", queryObj["VolumeName"]);
    }

Documentation .

    
21.03.2016 / 18:23