Question : Mapping a network drive in VB.NET or accessing a protected network path

I am trying to map a network drive, I can do so if there is no username and password setup on the directory. But if there is one I get Error code 1203 while using WNetAddConnection2.
 I cannot modify the directory to suit my code, it has to be the other way around.
Also I was wondering if there is a way of connecting to a network path with a username and a password and not worrying about mapping a drive letter. Any help in resolving this would be greatly appreciated.
This is my code so far.
And the network path is so
\\Drive\Folder\SubFolder\
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
    (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer

    Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" _
    (ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer


    Public Structure NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        Public lpLocalName As String
        Public lpRemoteName As String
        Public lpComment As String
        Public lpProvider As String
    End Structure

    Public Const ForceDisconnect As Integer = 1
    Public Const RESOURCETYPE_DISK As Long = &H1

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

        Dim nr As NETRESOURCE
        Dim strUsername As String
        Dim strPassword As String

        nr = New NETRESOURCE
        nr.lpRemoteName = UNCPath
        'nr.lpLocalName = DriveLetter & ":"
        strUsername = sUserName
        strPassword = sPassword 
        nr.dwType = RESOURCETYPE_DISK

        Dim result As Integer
        result = WNetAddConnection2(nr, strPassword, strUsername, 0)

        If result = 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    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

Answer : Mapping a network drive in VB.NET or accessing a protected network path

The only way I get this error is when I use the wrong username or password. This is how my example looks simply placed in a button. ASUS is the user account.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim nr As NETRESOURCE = Nothing
        Dim strUsername As String = String.Empty
        Dim strPassword As String = String.Empty
        Dim result As Integer = 0

        nr.lpRemoteName = "\\192.168.1.109\share"
        nr.lpLocalName = "R:"
        strUsername = "ASUS"
        strPassword = "password"
        nr.dwType = RESOURCETYPE_DISK

        result = WNetAddConnection2(nr, strPassword, strUsername, 0)
        Console.WriteLine(result)

    End Sub
Random Solutions  
 
programming4us programming4us