Network Mapping Batch File

ss7thirty

Well-known member
Joined
Jun 14, 2005
Messages
455
Location
New Jersey, US
Programming Experience
5-10
I am looking to create an executable or batch file that maps network drives automatically upon running. I have no idea how to go about this so maybe someone could help me out, show me a code snippet that maps a network drive, or possibly some suggestions or resources would be great. Thanks, everyone on this site is always so helpful.:D
 
Mmmm... I don't really understand why microsoft haven't provided a way to do this without scripting and so forth. So here's an example that uses the win32 api to map a network drive...

VB.NET:
 Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
( ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, _
ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer

Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" _
(ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer

    <StructLayout(LayoutKind.Sequential)> _
Public Structure NETRESOURCE
            Public dwScope As Integer
            Public dwType As Integer
            Public dwDisplayType As Integer
            Public dwUsage As Integer
            Public lpLocalName As String
            Public lpRemoteName As String
            Public lpComment As String
            Public lpProvider As String
    End Structure

Public Const ForceDisconnect As Integer = 1
Public Const RESOURCETYPE_DISK As Long = &H1

Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String) As Boolean
        
            Dim nr As NETRESOURCE
            Dim strUsername As String
            Dim strPassword As String

            nr = New NETRESOURCE
            nr.lpRemoteName = UNCPath
            nr.lpLocalName = DriveLetter & ":"
            strUsername = Nothing '(add parameters to pass this if necessary)
            strPassword = Nothing '(add parameters to pass this if necessary)
            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


Or to do this using scripting. There is a demo app in the below link that contains an app that can map all network drives. It costs a huge 2 pounds and i'm sure you can find a way to get a look at the source?!!

http://www.getdotnetcode.com/gdncstore/ProductDetails.aspx?productID=340
 
Thanks!

One other question that I have about that post is the functions that you are calling up top. I am not familiar with declare but I guess that means that you are calling a function that is inside that DLL file specified in the lib statement. If this is the case do I too have the mpr.dll file on my PC and if I do not what other way can I establish a connection.


Thanks for the code snippet and the resource boy I love this page. I just needed to get an idea of how I could accomplish this, I like to try to figure things out on my own though because that is the only way I can really learn. Everyone on this site has been so helpful. Thanks a lot man!
 
These functions are part of windows so don't worry they are already there. what these do is 'kind of' bypass the .Net framework and make a call directly to functions written by microsoft, although this isn't strictly correct, even though these functions make a call to unmanged code, it is all still marshalled by the CLR.
 
Easier through command prompt

Do you think it would be easier to accomplish this through the command prompt and using that type of file and if so what does that code look like, just a snippet may help. I'll be trying to google this also but.....

Thanks again :cool:
 
Back
Top