2

My VB6 program relies on data being on a network share. Win XP on a wireless network often cannot reconnect mapped drives at startup so they are in a disconnected state. The only way to reconnect them is to double-click on them in Explorer.

How can I do this programmatically? Is there an API call that will do it?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Craig Johnston
  • 7,467
  • 16
  • 40
  • 47

3 Answers3

2

You can use the WNetAddConnection function

Private Sub cmdMapDrive_Click()
Dim drive_letter As String
Dim share_name As String
Dim password As String

    lblResult.Caption = "Working..."
    Screen.MousePointer = vbHourglass
    DoEvents

    drive_letter = txtDriveLetter.Text
    If InStr(drive_letter, ":") = 0 _
        Then drive_letter = drive_letter & ":"
    share_name = txtShareName.Text
    password = txtPassword.Text

    If WNetAddConnection(share_name, password, _
        drive_letter) > 0 _
    Then
        lblResult.Caption = "Error mapping drive"
    Else
        lblResult.Caption = "Drive mapped"
    End If

    Screen.MousePointer = vbDefault
End Sub

Code Source: VB Helper

Adam Dempsey
  • 2,913
  • 5
  • 24
  • 26
1

You can use the dos command "net use" and start it with the shell-command from vb.

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/net_use.mspx?mfr=true

Tobias Schittkowski
  • 2,221
  • 2
  • 16
  • 25
0

I've done this with the Scripting.FileSystemObject:

Public Function MapDrive(ByVal Sharename As String, DriveToMap As String) As Boolean

    On Error GoTo Handler

    Dim fso As Scripting.FileSystemObject
    Dim ntwk As IWshRuntimeLibrary.IWshNetwork_Class

    ' Assume success; any failure will invoke the error handler & cause '
    ' the function to return false. '
    MapDrive = True

    Set fso = New Scripting.FileSystemObject
    Set ntwk = New IWshRuntimeLibrary.IWshNetwork_Class

    ' If the specified drive doesn't even exist, just map it '
    If Not fso.DriveExists(DriveToMap) Then
        ntwk.MapNetworkDrive DriveToMap, Sharename
        Exit Function
    End If

    ' The drive already exists; see if it's already be mapped correctly. '
    If UCase(fso.Drives(DriveToMap).ShareName) = UCase(Sharename) Then
        Exit Function
    End If

    ' The drive is mapped, but to the wrong place. Unmap, then map the drive. '
    ntwk.RemoveNetworkDrive DriveToMap
    ntwk.MapNetworkDrive DriveToMap, Sharename
    Exit Function

Handler:
    MapDrive = False
    Err.Clear

End Function
JeffK
  • 3,019
  • 2
  • 26
  • 29