How to Programmatically map a ftp dirve or folder as local disk in

Posted on 2006-05-12
Last Modified: 2012-05-05
i want to map a ftp server folder as a local drive in mycomputer programmatically throught . i have seen a similar question but its answers refferd to a software (webdrive) but i want to do it programmatically in .
Question by:oceanripples
    1 Comment
    LVL 5

    Accepted Solution

    .NET does not provide a way to map drives. To do this you still needs to access the WinAPI or use the net Use command. I have created two functions which demonstrate simple mapping and disconnecting(unmapping) of network drives using VB.NET and the Win32API.

           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
                    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
                    Return False
                End If

              End Function

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now