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.
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.
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.
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.
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.
“Net use” is a command line method of mapping network drives to your local computer.
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With