Disconnect and reconnect a network drive without user dialog in VB

By the way, here's the code:

Private Declare Function WNetAddConnection Lib "mpr.dll" Alias _
"WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword _
As String, ByVal lpszLocalName As String) As Long
Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias _
"WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Long) _
As Long
Sub Test()
Dim sDriveLetter As String
Dim sShareName As String
Dim sPassword As String
sDriveLetter = "G:"
sShareName = "\\Server1\Filearea1"
sPassword = ""
If WNetCancelConnection(sDriveLetter, 0) Then
MsgBox "Error disconnecting mapped drive"
Else
MsgBox "Drive mapping disconnected"
End If
If WNetAddConnection(sShareName, sPassword, sDriveLetter) > 0 Then
MsgBox "Error mapping drive"
Else
MsgBox "Drive mapped"
End If
End Sub
view raw mapnetdrive.vb hosted with ❤ by GitHub

Komentar