Resolved Rename a Mapped Drive

Tonus

Member
Joined
Aug 25, 2020
Messages
5
Programming Experience
Beginner
Hi Guys,

I am not proficient in VB although but can hack my away around from time to time.

At the moment, I am trying to write a very basic app that will allow a user to select a shared drive from a list and then click a button to map it. I have got this working quite nicely but despite searching I cannot find an example of how to rename the mapped drive so it has a friendly name e.g. instead of "X: \\server\share" it would show as "X: Main File Server".

I have found the below code which is VBS but I am not skilled enough to get this working in VB.NET (VS2019). Can anyone please help point me in the right direction?


VB.NET:
Dim drv, label
Dim shellApp
Dim objNet

Set objNet = CreateObject("Wscript.Network")
objNet.MapNetworkDrive "q:", "\\st106\data"

drv = "q:\" ' drive letter of mapped driver

label = "testtest" ' new label of mapped drive

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> THE BELOW IS WHAT I THINK I NEED IN MY VB.NET PROJECT BUT I DON'T KNOW HOW TO WRITE IT >>>>

Set shellApp = CreateObject("Shell.Application")

' set the drives label

shellApp.NameSpace(drv).Self.Name = label

WSCript.Quit
 
Last edited:
Line 14 and 18 works fine as is with late binding, just change Set to Dim
 
Thank you so much for quick reply and help. This has done the job (works perfectly) and below is the full code if anyone is interested...

VB.NET:
Imports System.Runtime.InteropServices


Public Class Form1

    Public Declare Function GetLogicalDriveStrings Lib "kernel32" _
     Alias "GetLogicalDriveStringsA" _
    (ByVal nBufferLength As Long,
     ByVal lpBuffer As String) As Long

    Dim DriveLetter As Char
    Dim RemotePath As String
    Dim VolumeLabel As String
    Dim shellApp = CreateObject("Shell.Application")

    <DllImportAttribute("mpr.dll",
      EntryPoint:="WNetAddConnection2W")>
    Public Shared Function WNetAddConnection2(ByRef lpNetResource _
      As NETRESOURCE, <InAttribute(),
      MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal _
      lpPassword As String, <InAttribute(),
      MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal _
      lpUserName As String, ByVal dwFlags As UInteger) As UInteger
    End Function

    <DllImportAttribute("mpr.dll",
      EntryPoint:="WNetCancelConnectionW")>
    Public Shared Function WNetCancelConnection(<InAttribute(),
      MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal _
      lpName As String, ByVal dwFlags As UInteger,
      <MarshalAsAttribute(UnmanagedType.Bool)> ByVal _
      fForce As Boolean) As UInteger
    End Function


    Public Const NO_ERROR As UInteger = 0
    Public Const RESOURCETYPE_DISK As UInteger = 1
    Public Const CONNECT_UPDATE_PROFILE As UInteger = 1

    <StructLayoutAttribute(LayoutKind.Sequential)>
    Public Structure NETRESOURCE

        Public dwScope As UInteger
        Public dwType As UInteger
        Public dwDisplayType As UInteger
        Public dwUsage As UInteger

        <MarshalAsAttribute(UnmanagedType.LPWStr)>
        Public lpLocalName As String

        <MarshalAsAttribute(UnmanagedType.LPWStr)>
        Public lpRemoteName As String

        <MarshalAsAttribute(UnmanagedType.LPWStr)>
        Public lpComment As String

        <MarshalAsAttribute(UnmanagedType.LPWStr)>
        Public lpProvider As String

    End Structure


    Public Shared Sub Map(ByVal strPath As String,
         ByVal strDrive As Char, ByVal blnPersist As Boolean,
         Optional ByVal strUser As String = Nothing,
         Optional ByVal strPassword As String = Nothing)

        Dim nrDrive As New NETRESOURCE

        With nrDrive

            .dwType = RESOURCETYPE_DISK
            .lpLocalName = strDrive & ":"
            .lpRemoteName = strPath

        End With

        Dim uiSet As UInteger = 0

        If blnPersist Then

            uiSet = &H1

        End If

        Dim uiRes As UInteger = WNetAddConnection2(nrDrive,
         strPassword, strUser, uiSet)

        If Not uiRes = NO_ERROR Then

            Throw New System.ComponentModel.Win32Exception _
            (CInt(uiRes))

        End If

    End Sub


    'Public Shared Sub Unmap(ByVal cDrive As Char)

    '    Dim uiRes As UInteger = WNetCancelConnection(cDrive & ":",
    '     CONNECT_UPDATE_PROFILE, True)

    '    If Not uiRes = NO_ERROR Then

    '        Throw New System.ComponentModel.Win32Exception _
    '        (CInt(uiRes))

    '    End If

    'End Sub

    'Private Sub Button2_Click(sender As Object, e As EventArgs) _
    '     Handles Button2.Click

    '    Unmap("T"c)

    'End Sub




    'Define Drives in List
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        DriveList.Items.Add("M:     FS01 Primary")
        DriveList.Items.Add("M:     FS02 Primary")
    End Sub


    Public Sub DriveList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DriveList.SelectedIndexChanged
        If DriveList.SelectedIndex = 0 Then
            DriveLetter = "M:"
            RemotePath = "\\server\D$"
            VolumeLabel = "Stuff"
        End If
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles MapDrive.Click
        Try

            Map(RemotePath, DriveLetter, True)
            shellApp.NameSpace("M:").Self.Name = "Stuff"


        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub


End Class
 
Hmm so think I was a bit premature in my excitement......

Seems if I hard-code the drive letter it works a treat but if I use the variable I get 'Conversion from type 'UShort' to type 'Char' is not valid.' error which I don't understand as the variable (DriveLetter) is defined as a Char and never changes.

This throws error -

VB.NET:
Map(RemotePath, DriveLetter, True)
shellApp.NameSpace(DriveLetter).Self.Name = VolumeLabel

This works -

VB.NET:
 Map(RemotePath, DriveLetter, True)
 shellApp.NameSpace("Q:").Self.Name = VolumeLabel
 
Hmm so think I was a bit premature in my excitement......

Seems if I hard-code the drive letter it works a treat but if I use the variable I get 'Conversion from type 'UShort' to type 'Char' is not valid.' error which I don't understand as the variable (DriveLetter) is defined as a Char and never changes.

This throws error -

VB.NET:
Map(RemotePath, DriveLetter, True)
shellApp.NameSpace(DriveLetter).Self.Name = VolumeLabel

This works -

VB.NET:
 Map(RemotePath, DriveLetter, True)
shellApp.NameSpace("Q:").Self.Name = VolumeLabel
Something isn't right there because the second code snippet is using a String containing two characters, not a Char. What is the actual value of DriveLetter when the exception is thrown? If it is "Q"c then you would need to make a String containing a colon out of that, which might be done like this:
VB.NET:
shellApp.NameSpace(DriveLetter & ":").Self.Name = VolumeLabel
or like this:
VB.NET:
shellApp.NameSpace($"{DriveLetter}:"").Self.Name = VolumeLabel
 
Something isn't right there because the second code snippet is using a String containing two characters, not a Char. What is the actual value of DriveLetter when the exception is thrown? If it is "Q"c then you would need to make a String containing a colon out of that, which might be done like this:
VB.NET:
shellApp.NameSpace(DriveLetter & ":").Self.Name = VolumeLabel
or like this:
VB.NET:
shellApp.NameSpace($"{DriveLetter}:").Self.Name = VolumeLabel

Amazing, first snippet resolved it thank you although I note your point about the Char vs String. When I defined DriveLetter as a string I actually got "'Object variable or With block variable not set.'" which I think indicates that the variable is empty. Again, not sure why but alas you have fixed it for me so thank you.
 
Last edited by a moderator:
Back
Top