Nicolas
Nicolas

Reputation: 412

How can I read the "friendly name" for a mapped network share?

Basics: C# WinForms desktop application targeting Dot Net 4.

I am enumerating all drives on a system using System.IO.DriveInfo. This works quite well and can also tell me what type of drive it is (fixed/network/cd-rom, etc). It can also give me the VolumeLabel - this works as desired on local drives, but for mapped network drives it gives you the VolumeLabel of the logical disk as defined on the host computer. In Windows Explorer, you can give any mapped drive a friendly name. Is there a way to programmatically retrieve this friendly name? This name is obviously the one that is familiar to the person using the computer.

For example, in the image below both "Critical" and "NonCritical" might be different shares on the same logical disk with a volume name "Data" on the host computer. Querying WMI for VolumeName returns "Data", and the other queries return just the drive letter, or else nothing.

Example

I have looked at the info available through WMI (see: How to programmatically discover mapped network drives on system and their server names?) but there doesn't seem to be a property that returns this friendly name. I've looked at Caption, Description, SystemName, and VolumeName - like this:

private void NetworkDrives()
{
    try
    {
        var searcher = new ManagementObjectSearcher(
            "root\\CIMV2",
            "SELECT * FROM Win32_MappedLogicalDisk");

        TreeNode share;
        TreeNode property;

        foreach (ManagementObject queryObj in searcher.Get())
        {
            share = new TreeNode("Name:" + queryObj["Name"]);                    
            share.Nodes.Add("Caption: " + queryObj["Caption"]);
            share.Nodes.Add("Description: " + queryObj["Description"]);
            share.Nodes.Add("SystemName: " + queryObj["SystemName"]);
            share.Nodes.Add("VolumeName: " + queryObj["VolumeName"]);
            share.Nodes.Add("DeviceID: " + queryObj["DeviceID"]);
            treeView1.Nodes.Add(share);
        }
    }
    catch (ManagementException ex)
    {
        MessageBox.Show("An error occurred while querying for WMI data: " + ex.Message);
    }
}

Thanks in advance.

Upvotes: 6

Views: 3712

Answers (2)

Nicolas
Nicolas

Reputation: 412

The correct values are stored in the Registry in the value _LabelFromReg of the key HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints‌​2\##ServerName#ShareName. This value only exists for a share if you have relabeled that share.

EDIT: Here is a working example, albeit without proper error checking. Thanks JPBlanc for the registry shortcut to use HKEY_CURRENT_USER\etc instead of HKEY_USERS\<SID>\etc.

private string GetDriveLabel(DriveInfo drv)
{
    string drvName;
    string drvLabel;
    string pvdr = "";

    //Start off with just the drive letter
    drvName = "(" + drv.Name.Substring(0,2) + ")";

    //Use the volume label if it is not a network drive
    if (drv.DriveType != DriveType.Network)
    {
        drvLabel = drv.VolumeLabel;
        return drvLabel + " " + drvName;
    }

    //Try to get the network share name            
    try
    {
        var searcher = new ManagementObjectSearcher(
            "root\\CIMV2",
            "SELECT * FROM Win32_MappedLogicalDisk WHERE Name=\"" + drv.Name.Substring(0,2) + "\"");

        foreach (ManagementObject queryObj in searcher.Get())
        {
            pvdr = @queryObj["ProviderName"].ToString();
        }
    }
    catch (ManagementException ex)
    {
        pvdr = "";
    }

    //Try to get custom label from registry
    if (pvdr != "")
    {   
        pvdr = pvdr.Replace(@"\", "#");
        drvLabel = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\" + pvdr, "_LabelFromReg", "");
        if (string.IsNullOrEmpty(drvLabel))
        {
            //If we didn't get the label from the registry, then extract the share name from the provider
            drvLabel = pvdr.Substring(pvdr.LastIndexOf("#") + 1);
        }
        return drvLabel + " " + drvName;
    }
    else
    {
        //No point in trying the registry if we don't have a provider name
        return drvName;
    }
}

Upvotes: 2

JPBlanc
JPBlanc

Reputation: 72630

Using Win32_Logicaldisk you've got :

DeviceID     : P:
DriveType    : 4
ProviderName : \\localhost\download\Chic
FreeSpace    : 26406707200
Size         : 500000878592
VolumeName   :

For DriveType 4, you will find the Volume in the last part of the UNC name. You just have to use a regular exprssion to get it.


Edited according to your comment.

Using PowerShell (not far from C#) it can be written :

cd HKCU:
Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2 | where {$_.name -like '*##localhost#download#Chic'} | % {(Get-ItemProperty -LiteralPath $_.name -Name _LabelFromReg)._LabelFromReg} 

Upvotes: 2

Related Questions