Retrieve volume name from networked mapped disk drive

I am using DriveInfo.GetDrivers() to take the names of the disk drives present on the machine and list them in a TreeView.

I made the code below that works, but it does not appear the names of network disks that my machine has access, only 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 the drv.VolumeLabel that the networked disks stop appearing (concatenate to appear to the user the disk name for identification)

I'm thinking of a if to display them, however, the goal was to display the name of the disk in network, since much of the work will be on different networks.

Update:

I did a If to correctly display the disk name and display at least the letter of the DrIve network

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:

insert the description of the image here

The problem is that in this medium ai has pendrive connected, network HD and CD-Rom disk in the middle.

Author: Maniero, 2016-03-21

1 answers

Apparently the way to do this is with WMI, according to this answer in the OS. Test with this code to see if it returns what you want and adapts to what you need.

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

I put on GitHub for future reference.

Documentation .

 3
Author: Maniero, 2020-10-19 15:59:34