Drive Map and UnMap

    Public Function UnMapDrive(ByVal DriveLetter As String) As Boolean

        Dim rc As Integer

        rc = WNetCancelConnection2(DriveLetter & ":", 0, ForceDisconnect)

        If rc = 0 Then

            Return True

        Else

            Return False

        End If

    End Function


    Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String, strUsername As String, strPassword As String) As Boolean

        Dim nr As NETRESOURCE

        'Dim strUsername As String

        'Dim strPassword As String

        nr = New NETRESOURCE

        nr.dwScope = RESOURCE_GLOBALNET

        nr.lpRemoteName = UNCPath

        nr.lpLocalName = DriveLetter & ":"

        'strUsername = Nothing '(add parameters to pass this if necessary)

        'strPassword = Nothing '(add parameters to pass this if necessary)

        nr.dwType = RESOURCETYPE_ANY 'RESOURCETYPE_DISK

        nr.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE

        nr.dwUsage = RESOURCEUSAGE_CONNECTABLE

        Dim result As Integer

        result = WNetAddConnection2(nr, strPassword, strUsername, 0)

        If result = 0 Then

            Return True

        Else

            Select Case result

                Case ERROR_BAD_NETPATH

                    funWriteWordinColor("QA4001I Bad path could not connect to Star Directory" & vbCrLf, ConsoleColor.Red)

                Case ERROR_INVALID_PASSWORD

                    funWriteWordinColor("QA4002I Invalid password could not connect to Star Directory" & vbCrLf, ConsoleColor.Red)

                Case ERROR_NETWORK_ACCESS_DENIED

                    funWriteWordinColor("QA4003I Network access denied could not connect to Star Directory" & vbCrLf, ConsoleColor.Red)

                Case ERROR_NETWORK_BUSY

                    funWriteWordinColor("QA4004I Network busy could not connect to Star Directory" & vbCrLf, ConsoleColor.Red)

                Case Else

                    funWriteWordinColor("Mapping Error: " & result.ToString & vbCrLf, ConsoleColor.Red)

            End Select

            Return False

        End If

    End Function

    'http://www.mredkj.com/vbnet/vbnetmapdrive.html

    ''https://support.microsoft.com/en-us/help/968264/error-message-when-you-try-to-map-to-a-network-drive-of-a-dfs-share-by

 

System error 1312 has occurred.
A specified logon session does not exist. It may already have been terminated.  

Cause

This behavior occurs when the "Do not allow storage of credentials or .NET Passports for network authentication" Network access policy is enabled.

Workaround
To work around this behavior, follow these steps:
  1. On the Management Server, click Start, click Run, type gpedit.msc, and then click OK.
  2. Under Computer Configuration, expand Windows Settings, expand Security Settings, expand Local Policies, and then expand Security Options.
  3. In the Policy pane, right-click Network access: Do not allow storage of credentials or .NET Passports for network authentication, click Properties, click Disabled, and then click OK. The corresponding registry name and location is:
    Location: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\
    Name: DisableDomainCreds
    Value: 0 (DWORD)