How to connect to network drive in VB.net

Imports System.Runtime.InteropServices
Imports System.IO
 
Public Class WNetAddConnection2
 
    Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As StringByVal lpUserName As StringByVal dwFlags As IntegerAs Integer
    Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" (ByVal lpName As StringByVal dwFlags As IntegerByVal fForce As IntegerAs Integer
 
    Public Const ForceDisconnect As Integer = 1
    Public Const RESOURCETYPE_DISK As Long = &H1 ' 'ディスク
    Public Const CONNECT_UPDATE_PROFILE = &H1 '次回ログオン時に再接続
 
    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
 
    'Function to map drive/connect to drive
    Public Shared Function MapDrive(ByVal UNCPath As StringByVal strUserName As StringByVal strPassword As StringAs Boolean
        If Directory.Exists(UNCPath) Then Return True
 
        Dim nr As New NETRESOURCE
        With nr
            .lpRemoteName = UNCPath
            .lpLocalName = String.Empty
            .dwType = RESOURCETYPE_DISK
        End With
 
        ''次回ログオン時に再接続の場合
        'Dim result As Integer = = WNetAddConnection2(nr, strUsername, strPassword, CONNECT_UPDATE_PROFILE)
 
        ''次回ログオン時に再接続しない場合
        Dim result As Integer = WNetAddConnection2(nr, strUserName, strPassword, 0)
 
        ''ユーザ名、パスワードが不要な場合
        'Dim result As Integer = WNetAddConnection2(nr, vbNullString, vbNullString, 0)
 
        If result = 0 Then
            Return True
        Else
            Return False
        End If
 
    End Function
 
    'Function to disconnect from drive
    Public Shared Function UnMapDrive(ByVal UNCPath As StringAs Boolean
        Dim rc As Integer = WNetCancelConnection2(UNCPath, 0, ForceDisconnect)
 
        If rc = 0 Then
            Return True
        Else
            Return False
        End If
    End Function