I use the in my services.
Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
UnMapDrive("P")
If Not MapDrive("P", mstrQuasarFileLocation, _
"username", "password") Then
EventLog1.WriteEntry("Unable to map drive")
UnMapDrive("P")
End If
End Sub
Public Function MapDrive(ByVal DriveLetter As String, _
ByVal UNCPath As String, _
ByVal strUsername As String, _
ByVal strPassword As String) As Boolean
Dim nr As NETRESOURCE
nr = New NETRESOURCE
nr.lpRemoteName = UNCPath
nr.lpLocalName = DriveLetter & ":"
strUsername = strUsername
strPassword = strPassword
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