Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 471
Nicolas Avatar asked Feb 25 '14 13:02

Nicolas


People also ask

How do I find the name of a shared network drive?

Open Command Prompt. Then type the command net share and hit Enter to continue. Then the shared folder will be listed. From the command line, you can also find the path of the shared folders.

What Windows command displays currently mapped shares?

Use the MS-DOS "net share" command To use this command, follow the steps below. Click Start, Run, type cmd, and press Enter . At the MS-DOS prompt, type net share and press Enter . Each of the shares, the location of the resource, and any remarks for that share are displayed.

How do I show network mapping?

Open File Explorer from the taskbar or the Start menu, or press the Windows logo key + E. Select This PC from the left pane. Then, on the Computer tab, select Map network drive. In the Drive list, select a drive letter.

What is the command used to map a network share?

“Net use” is a command line method of mapping network drives to your local computer.


1 Answers

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} 
like image 138
JPBlanc Avatar answered Oct 18 '22 20:10

JPBlanc