Link to home
Start Free TrialLog in
Avatar of Khel4Me
Khel4MeFlag for India

asked on

Add network share

Hi Experts,
I want to add network share so that it can be available for use and I am using following code but it is not working. I search a lot but could not fould anything helpful.

Please guide...
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
        Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
        Public Const RESOURCETYPE_DISK = &H1
        Public Const RESOURCETYPE_ANY = &H0
        Public Const RESOURCE_GLOBALNET = &H2
        Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
        Public Const RESOURCEUSAGE_CONNECTABLE = &H1
        Public Const CONNECT_UPDATE_PROFILE = &H1
 
        Private Structure NETRESOURCE
            Public dwScope As Long
            Public dwType As Long
            Public dwDisplayType As Long
            Public dwUsage As Long
            Public lpLocalName As String
            Public lpRemoteName As String
            Public lpComment As String
            Public lpProvider As String
        End Structure
        Public Enum ErrorType As UInteger
            ERROR_ACCESS_DENIED = 5&
            ERROR_ALREADY_ASSIGNED = 85&
            ERROR_BAD_DEV_TYPE = 66&
            ERROR_BAD_DEVICE = 1200&
            ERROR_BAD_NET_NAME = 67&
            ERROR_BAD_PROFILE = 1206&
            ERROR_BAD_PROVIDER = 1204&
            ERROR_BUSY = 170&
            ERROR_CANCELLED = 1223&
            ERROR_CANNOT_OPEN_PROFILE = 1205&
            ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
            ERROR_EXTENDED_ERROR = 1208&
            ERROR_INVALID_PASSWORD = 86&
            ERROR_NO_NET_OR_BAD_PATH = 1203&
        End Enum
 
Private Sub AddShare()
            Dim nr As NETRESOURCE = Nothing
            Dim l As Long
 
            With nr
                .dwType = RESOURCETYPE_ANY 'RESOURCETYPE_DISK
                .lpLocalName = ""
                .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
                .lpRemoteName = "\\MyServerIP\MyShare"
                .dwUsage = RESOURCEUSAGE_CONNECTABLE
                '.lpProvider = ""
                .dwScope = RESOURCE_GLOBALNET
            End With
 
                l = WNetAddConnection2(nr, ConOption.Password, ConOption.Username, CONNECT_UPDATE_PROFILE)
End Sub

Open in new window

Avatar of cheeselover73
cheeselover73
Flag of Serbia image

Does it have to be Visual Basic? Simple .bat file could do it.
Avatar of Khel4Me

ASKER

yes I know simple .bat file can do that but I need vb.net code. The code is right but not working on my machine.
thanks,
Amit
ASKER CERTIFIED SOLUTION
Avatar of Khel4Me
Khel4Me
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial