Results 1 to 13 of 13

Thread: Mapping a network drive

Threaded View

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Mapping a network drive

    Unfortunately in the .NET Framework there is currently no way to create a network drive (aka mapped drive) so we have to use a Windows API to accomplish this, the WNetAddConnection2 API to be precise. So I thought I would provide the VB.NET definitions for the API function and the relevant structures/constants, along with a .NET method that you can use to wrap this API functionality to make it easier to use

    EDIT: The .NET wrapper for this function and loads of others are all in my .NET Windows API Library which you can download here to save copying and pasting API definitions etc into your projects: http://cjwdev.wordpress.com/2011/06/...-2-2-released/

    First of all make sure you use this at the very top of your code:
    vb Code:
    1. Imports System.Runtime.InteropServices
    then you can copy and paste these API definitions, which I've added comments to just in case you do want to use these directly instead of using the .NET method I will provide later in this post.
    vb Code:
    1. 'Constants
    2.     Public Const NO_ERROR As UInteger = 0
    3.     Public Const RESOURCETYPE_DISK As UInteger = 1
    4.  
    5.     ''' <summary>
    6.     ''' Creates a connection to a network resource
    7.     ''' </summary>
    8.     ''' <param name="lpNetResource">A NETRESOURCE structure that specifies information about the network resource connection</param>
    9.     ''' <param name="lpPassword">The password to use for the connection - leave blank to use current user credentials</param>
    10.     ''' <param name="lpUserName">The username to use for the connection - leave blank to use current user credentials</param>
    11.     ''' <param name="dwFlags">Bitmask that specifies connection options. For example, whether or not to make the connection persistent</param>
    12.     <DllImportAttribute("mpr.dll", EntryPoint:="WNetAddConnection2W")> _
    13.     Public Shared Function WNetAddConnection2(ByRef lpNetResource As NETRESOURCE, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpPassword As String, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpUserName As String, ByVal dwFlags As UInteger) As UInteger
    14.     End Function
    15.  
    16.     ''' <summary>
    17.     ''' Contains information about a network resource. Used by the WNetAddConnection2 method
    18.     ''' </summary>
    19.     <StructLayoutAttribute(LayoutKind.Sequential)> _
    20.     Public Structure NETRESOURCE
    21.         Public dwScope As UInteger
    22.         Public dwType As UInteger
    23.         Public dwDisplayType As UInteger
    24.         Public dwUsage As UInteger
    25.         <MarshalAsAttribute(UnmanagedType.LPWStr)> _
    26.         Public lpLocalName As String
    27.         <MarshalAsAttribute(UnmanagedType.LPWStr)> _
    28.         Public lpRemoteName As String
    29.         <MarshalAsAttribute(UnmanagedType.LPWStr)> _
    30.         Public lpComment As String
    31.         <MarshalAsAttribute(UnmanagedType.LPWStr)> _
    32.         Public lpProvider As String
    33.     End Structure

    and here is the .NET method I wrote that makes using the API much simpler:

    vb Code:
    1. ''' <summary>
    2.     ''' Creates a network drive (aka mapped drive) using the specified drive letter, UNC path and optional credentials
    3.     ''' </summary>
    4.     ''' <param name="UncPath">The UNC path (\\servername\share) to map the drive letter to</param>
    5.     ''' <param name="DriveLetter">The drive letter to use</param>
    6.     ''' <param name="Persistent">False to have this drive removed when the user logs off. True to have the drive remembered.
    7.     ''' This option is the equivelant of the Reconnect At Logon checkbox shown when mapping a drive in Windows Exporer</param>
    8.     ''' <param name="ConnectionUsername">The username to use for the connection - optional</param>
    9.     ''' <param name="ConnectionPassword">The password to use for the connection - optional</param>
    10.     Public Shared Sub MapNetworkDrive(ByVal UncPath As String, ByVal DriveLetter As Char, ByVal Persistent As Boolean, Optional ByVal ConnectionUsername As String = Nothing, Optional ByVal ConnectionPassword As String = Nothing)
    11.         If String.IsNullOrEmpty(UncPath) Then
    12.             Throw New ArgumentException("No UNC path specified", "UncPath")
    13.         End If
    14.         Dim DriveInfo As New NETRESOURCE
    15.         With DriveInfo
    16.             .dwType = RESOURCETYPE_DISK
    17.             .lpLocalName = DriveLetter & ":"
    18.             .lpRemoteName = UncPath
    19.         End With
    20.         Dim flags As UInteger = 0
    21.         If Persistent Then
    22.             flags = &H1
    23.         End If
    24.         Dim Result As UInteger = WNetAddConnection2(DriveInfo, ConnectionPassword, ConnectionUsername, flags)
    25.         If Not Result = NO_ERROR Then
    26.             Throw New System.ComponentModel.Win32Exception(CInt(Result))
    27.         End If
    28.     End Sub

    so then when you want to map a network drive you can simply do this:

    vb Code:
    1. 'This example maps the R drive to \\SomeServer\SomeShare
    2. Try
    3.      MapNetworkDrive("\\SomeServer\SomeShare", "R"c, False)
    4. Catch ex As Exception
    5.      MessageBox.Show("The network drive could not be mapped for the following reason: " & ex.Message)
    6. End Try
    Last edited by chris128; Sep 11th, 2013 at 12:55 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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