Recover Volume Name of Netmapped Disk Drive

Asked

Viewed 386 times

4

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

I made the code below that works, however it does not appear the names of disks in network 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 concaten with the drv.VolumeLabel that networked disks stop appearing (concateno to appear to the user the name of the disk for identification)

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

Updating:

I made a If to correctly display the disk name and display at least the dr letterinet ve

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:

inserir a descrição da imagem aqui

The problem is that in this middle there is flash drive connected, networked HD and CD-Rom disk in the middle.

  • When it’s a drive network, the drv.VolumeLabel has content or is void?

  • drv.Volumelabel content is "Data".

  • Did that solve it? I guess that’s what I was thinking.

  • So, I need to show the user the name of the network drive, instead of the letter, because it’s a lot of letters, take a look at the image I’m going to ask the question.

1 answer

3


Apparently the way to do this is with WMI, according to this response in the OS. 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()) WriteLine("VolumeName: {0}", queryObj["VolumeName"]);

I put in the Github for future reference.

Documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.