locked
Get Mapped Drive Label?

    Question

  • I am able to get the path to a mapped drive and retrieve information about it, except I can't seem to retrieve the actual label as Windows Explorer displays it. 

    I thought drive.sharename would be appropriate but it returns the full path to the network path.  I want the label as displayed in Explorer.  Thanks in advance.

            Dim fso As New Scripting.FileSystemObject()
            Dim drive As Scripting.Drive

            For Each drive_info As DriveInfo In DriveInfo.GetDrives()

                If drive_info.DriveType = DriveType.Network Then
                    drive = fso.GetDrive(fso.GetDriveName(drive_info.Name))
                    console.WriteLine(drive.ShareName) 
                End If
            Next
    Tuesday, November 20, 2007 6:42 PM

Answers

  • Hi Noneplease,

     

    Based on your post, you need to get the label of the network drive.

     

    In this scenario I don't think that we can get this label from the DriveInfo class. Initially I used the WMI to query all information about the network drive, but this can't return the required information. Finally I find that we can get this information from the registry. You set the label to a network drive to a special name. Then use the Find menu item to search the appearance this name. You can find the information which locates in key HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\
    Explorer\MountPoints2\. Now you can use the RegistryKey class to get this required information. Hope this helps.

     

    Best regards,

    Riquel

     

    Friday, November 23, 2007 5:40 AM
    Moderator
  • Thanks for the info.  That was close but just looking at that one location doesn't give you the drive associated with the particular label.  Further searching in the registry revealed that you perform the following.


    This key has subkeys that list each network drive letter.
    HKEY_CURRENT_USER\Network
    Retrieve the value for RemotePath for the particular drive letter you want (Z:\) would be "Z"
    HKEY_CURRENT_USER\Network\Z\RemotePath

    in my example it returns:   \\networkserver\shared

    replace each instance of "\" with "#" so you now have a returned string of

    ##networkserver#shared


    Then use the key below (in my case it was HKCU, HKLM as you said)

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##networkserver#shared

    Retrieve the value for _LabelFromReg

    That gives you the properly formatted label for each drive (at least in XP)

    A rough code example is included below.

        Private Function GetDriveNetworkLabel(ByVal Drive As DriveInfo) As String
            Dim TempNetwork As String
            Dim SecondTempNetwork As String

            TempNetwork = Strings.Replace(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Network\" & Strings.Left(Drive.Name, 1), "RemotePath", ""), "\", "#")
            SecondTempNetwork = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\" & TempNetwork, "_LabelFromReg", "")

            Return SecondTempNetwork

        End Function


    Friday, November 23, 2007 7:31 AM

All replies

  • Hi Noneplease,

     

    Based on your post, you need to get the label of the network drive.

     

    In this scenario I don't think that we can get this label from the DriveInfo class. Initially I used the WMI to query all information about the network drive, but this can't return the required information. Finally I find that we can get this information from the registry. You set the label to a network drive to a special name. Then use the Find menu item to search the appearance this name. You can find the information which locates in key HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\
    Explorer\MountPoints2\. Now you can use the RegistryKey class to get this required information. Hope this helps.

     

    Best regards,

    Riquel

     

    Friday, November 23, 2007 5:40 AM
    Moderator
  • Thanks for the info.  That was close but just looking at that one location doesn't give you the drive associated with the particular label.  Further searching in the registry revealed that you perform the following.


    This key has subkeys that list each network drive letter.
    HKEY_CURRENT_USER\Network
    Retrieve the value for RemotePath for the particular drive letter you want (Z:\) would be "Z"
    HKEY_CURRENT_USER\Network\Z\RemotePath

    in my example it returns:   \\networkserver\shared

    replace each instance of "\" with "#" so you now have a returned string of

    ##networkserver#shared


    Then use the key below (in my case it was HKCU, HKLM as you said)

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##networkserver#shared

    Retrieve the value for _LabelFromReg

    That gives you the properly formatted label for each drive (at least in XP)

    A rough code example is included below.

        Private Function GetDriveNetworkLabel(ByVal Drive As DriveInfo) As String
            Dim TempNetwork As String
            Dim SecondTempNetwork As String

            TempNetwork = Strings.Replace(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Network\" & Strings.Left(Drive.Name, 1), "RemotePath", ""), "\", "#")
            SecondTempNetwork = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\" & TempNetwork, "_LabelFromReg", "")

            Return SecondTempNetwork

        End Function


    Friday, November 23, 2007 7:31 AM