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 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 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 String, ByVal strUserName As String, ByVal strPassword As String) As 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 String) As Boolean Dim rc As Integer = WNetCancelConnection2(UNCPath, 0, ForceDisconnect) If rc = 0 Then Return True Else Return False End If End Function