Results 1 to 2 of 2

Thread: Map network drive

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Red face

    How would i go about mapping \\lab_techs\c\ when my program starts up? If possible assign it to letter T but if you just know how to map that directory it would be a big help thanks.
    -RaY
    VB .Net 2010 (Ultimate)

  2. #2
    Lively Member
    Join Date
    Aug 2000
    Location
    quebec
    Posts
    81

    Cool

    The WNetAddConnection function works and is simple.

    However it is now obsolete (16-bit) and you should use
    WNetAddConnection2 which is a little more complexe.

    Here's some code that works - just copypaste it into a form
    with a command button 'cmdMapDrive' and change the strNetResName var:
    '================================
    Option Explicit

    Public Type NETRESOURCE
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    lpLocalName As String
    lpRemoteName As String
    lpComment As String
    lpProvider As String
    End Type
    '--------------------
    ' some error values - there's loads of these so check 'em out
    Const ERROR_INVALID_PASSWORDNAME = 1216&
    Const ERROR_ACCESS_DENIED = 5&
    Const ERROR_BAD_DEVICE = 1200&
    Const ERROR_BAD_PROFILE = 1206&
    Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
    Const ERROR_DEV_NOT_EXIST = 55&
    Const ERROR_ALREADY_ASSIGNED = 85&
    Const NO_ERROR = 0
    '--------------------
    Const RESOURCETYPE_DISK As Long = &H1& 'dwType
    '--------------------
    Private Declare Function WNetAddConnection2 Lib "mpr.dll" _
    Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, _
    ByVal lpPassword As String, _
    ByVal lpUserName As String, _
    ByVal dwFlags As Long) As Long
    ' dwFlags
    Const CONNECT_UPDATE_PROFILE As Long = &H1&
    '--------------------
    Dim netRes As NETRESOURCE
    Dim lResult As Long

    ' connect to the network drive
    Private Sub cmdMapDrive_Click()
    Dim strDriveLetter, strNetResName As String
    Dim strPswd, strUser As String

    strDriveLetter = "T:" + Chr$(0)
    strNetResName = "\\some_network_computer\some_dir" + Chr(0)

    With netRes
    .dwType = RESOURCETYPE_DISK
    .lpLocalName = strDriveLetter
    .lpRemoteName = strNetResName
    .lpProvider = ""
    End With
    ' don't need to fill in the other fields in most cases

    lResult = WNetAddConnection2(netRes, _
    strPswd, _
    strUser, _
    CONNECT_UPDATE_PROFILE) '// update profile with connect information

    ' check for errors using const's defined above
    Select Case lResult
    Case NO_ERROR
    ' no problem
    'Case any other erros
    '.
    '.
    '.
    Case Else
    'problem
    End Select

    End Sub
    '===========================
    You should put a lot of this in a module or maybe make a
    class out for it and include adequate error checking and
    parameters etc. for modularity.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width