Hi Li, I could not find a way to do this using Managed code, but found the
following :
Private mblnConnected As Boolean = False
Private mstrDriveMapped To As String = "" ' in format "X:\"
Private mstrUNCPath As String
Private Declare Function WNetAddConnecti on2 Lib "mpr.dll" Alias
"WNetAddConnect ion2A" (ByVal netResource As NETRESOURCE, ByVal password As
[String], ByVal Username As [String], ByVal Flag As Integer) As Integer
Private Declare Function WNetCancelConne ction2 Lib "mpr.dll" Alias
"WNetCancelConn ection2A" (ByVal lpName As String, ByVal dwFlags As Integer,
ByVal fForce As Integer) As Integer
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA "
(ByVal flags As Integer, ByRef source As Object, ByVal messageID As Integer,
ByVal languageID As Integer, ByVal buffer As String, ByVal size As Integer,
ByRef arguments As Integer) As Integer
Public Sub Connect(ByVal ServerShare As String, ByVal User As String, ByVal
Password As String, ByVal SubDirectoryNam e As String)
Dim myNetResource As New NETRESOURCE()
Dim errorText As String
If ServerShare.End sWith("\") Then ServerShare =
ServerShare.Rem ove(ServerShare .Length - 1, 1)
myNetResource.d wScope = 2 'RESOURCE_GLOBA LNET
myNetResource.d wType = 1 'RESOURCETYPE_D ISK
myNetResource.d wDisplayType = 3 'RESOURCEDISPLA YTYPE_SHARE
myNetResource.d wUsage = 1 'RESOURCEUSAGE_ CONNECTABLE
myNetResource.L ocalName = Nothing 'Or use "P:" for mapped drive
myNetResource.R emoteName = ServerShare
myNetResource.P rovider = Nothing
Dim ret As Integer = WNetAddConnecti on2(myNetResour ce, Password, User, 0)
If ret <> 0 Then
errorText = FormatErrorMess age(ret)
Throw New ApplicationExce ption("Unable to connect network drive. Win32 error
code is: " & ret.ToString & " (" & errorText & ")")
End If
mstrDriveMapped To = ServerShare
mstrUNCPath = ServerShare
If SubDirectoryNam e <> String.Empty Then
mstrDriveMapped To &= "\" & SubDirectoryNam e
End If
End Sub
Public Sub Disconnect()
Dim CONNECT_UPDATE_ PROFILE As Integer = 1
Dim FORCE_DRIVE_CLO SE As Integer = 1
Dim errorText As String
Dim ret As Integer = WNetCancelConne ction2(mstrUNCP ath,
CONNECT_UPDATE_ PROFILE, FORCE_DRIVE_CLO SE)
If ret <> 0 Then
errorText = FormatErrorMess age(ret)
Throw New ApplicationExce ption("Unable to disconnect network drive. Win32
error code is: " & ret.ToString & " (" & errorText & ")")
End If
mstrDriveMapped To = String.Empty
End Sub
Public ReadOnly Property DriveMappedTo() As String
Get
Return mstrDriveMapped To
End Get
End Property
Private Function FormatErrorMess age(ByVal Win32ErrorCode As Integer) As
String
Const FORMAT_MESSAGE_ FROM_SYSTEM As Short = &H1000
Const LANG_NEUTRAL As Short = &H0
Dim buffer As String = Space(999)
Try
FormatMessage(F ORMAT_MESSAGE_F ROM_SYSTEM, 0, Win32ErrorCode, LANG_NEUTRAL,
buffer, 999, 0)
buffer = Replace(Replace (buffer, Chr(13), String.Empty), Chr(10),
String.Empty)
Return buffer.Substrin g(0, buffer.IndexOf( Chr(0)))
Catch
Return "Unable to determine error text"
End Try
End Function
<StructLayout(L ayoutKind.Seque ntial)> _
Public Class NETRESOURCE
Public dwScope As Integer
Public dwType As Integer
Public dwDisplayType As Integer
Public dwUsage As Integer
Public LocalName As String
Public RemoteName As String
Public Comment As String
Public Provider As String
End Class 'NETRESOURCE
"Li Pang" <Li****@discuss ions.microsoft. com> wrote in message
news:D0******** *************** ***********@mic rosoft.com...
Hi,
I'd like to know how to easily map a network drive using vb.net.
thanks