Microsoft & .NETVisual BasicAdd a Network Drive

Add a Network Drive content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This tip shows you how to add a new mapped network drive to the local computer in your code:

Option Explicit

Declare Function WNetAddConnection_
Lib "mpr.dll" Alias_
(ByVal lpszNetPath_As String,_
ByVal lpszPassword As String,_
ByVal lpszLocalName As String)_
As Long

Declare Function_
Lib "mpr.dll" Alias_
(ByVal lpszName As String, ByVal_
bForce As Long) As Long

Const WN_SUCCESS = 0_
' The function was successful.
Const WN_NET_ERROR = 2_
' An error occurred on the network.
' The password was invalid.

Function AddConnection(MyShareName_
As String, MyPWD As _String,_
UseLetter As String) As Integer

On Local Error GoTo_

AddConnection = WNetAddConnection_
(MyShareName, MyPWD, UseLetter)

Exit Function
AddConnection = Err
MsgBox Error$
Resume AddConnection_End
End Function

Private Sub cmdAdd_Click()
AddConnection txtShareName.Text,_
txtPassword.Text ,_
End Sub

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories