How map a network drive

J

JohnFol

Hi Li, I could not find a way to do this using Managed code, but found the
following :



Private mblnConnected As Boolean = False

Private mstrDriveMappedTo As String = "" ' in format "X:\"

Private mstrUNCPath As String

Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias
"WNetAddConnection2A" (ByVal netResource As NETRESOURCE, ByVal password As
[String], ByVal Username As [String], ByVal Flag As Integer) As Integer

Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias
"WNetCancelConnection2A" (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 SubDirectoryName As String)

Dim myNetResource As New NETRESOURCE()

Dim errorText As String

If ServerShare.EndsWith("\") Then ServerShare =
ServerShare.Remove(ServerShare.Length - 1, 1)

myNetResource.dwScope = 2 'RESOURCE_GLOBALNET

myNetResource.dwType = 1 'RESOURCETYPE_DISK

myNetResource.dwDisplayType = 3 'RESOURCEDISPLAYTYPE_SHARE

myNetResource.dwUsage = 1 'RESOURCEUSAGE_CONNECTABLE

myNetResource.LocalName = Nothing 'Or use "P:" for mapped drive

myNetResource.RemoteName = ServerShare

myNetResource.Provider = Nothing

Dim ret As Integer = WNetAddConnection2(myNetResource, Password, User, 0)

If ret <> 0 Then

errorText = FormatErrorMessage(ret)

Throw New ApplicationException("Unable to connect network drive. Win32 error
code is: " & ret.ToString & " (" & errorText & ")")

End If

mstrDriveMappedTo = ServerShare

mstrUNCPath = ServerShare

If SubDirectoryName <> String.Empty Then

mstrDriveMappedTo &= "\" & SubDirectoryName

End If

End Sub

Public Sub Disconnect()

Dim CONNECT_UPDATE_PROFILE As Integer = 1

Dim FORCE_DRIVE_CLOSE As Integer = 1

Dim errorText As String

Dim ret As Integer = WNetCancelConnection2(mstrUNCPath,
CONNECT_UPDATE_PROFILE, FORCE_DRIVE_CLOSE)

If ret <> 0 Then

errorText = FormatErrorMessage(ret)

Throw New ApplicationException("Unable to disconnect network drive. Win32
error code is: " & ret.ToString & " (" & errorText & ")")

End If

mstrDriveMappedTo = String.Empty

End Sub

Public ReadOnly Property DriveMappedTo() As String

Get

Return mstrDriveMappedTo

End Get

End Property

Private Function FormatErrorMessage(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(FORMAT_MESSAGE_FROM_SYSTEM, 0, Win32ErrorCode, LANG_NEUTRAL,
buffer, 999, 0)

buffer = Replace(Replace(buffer, Chr(13), String.Empty), Chr(10),
String.Empty)

Return buffer.Substring(0, buffer.IndexOf(Chr(0)))

Catch

Return "Unable to determine error text"

End Try

End Function

<StructLayout(LayoutKind.Sequential)> _

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
 
G

Guest

Thank JohnFol for your response and sample code. I just find an alternative
way to figure out my problem by running wscript within VB.NET. It works well.
Using WNetAddConnection2 is a VB6 way, I pretty sure it works in VB.NET as
well.

Li

JohnFol said:
Hi Li, I could not find a way to do this using Managed code, but found the
following :



Private mblnConnected As Boolean = False

Private mstrDriveMappedTo As String = "" ' in format "X:\"

Private mstrUNCPath As String

Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias
"WNetAddConnection2A" (ByVal netResource As NETRESOURCE, ByVal password As
[String], ByVal Username As [String], ByVal Flag As Integer) As Integer

Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias
"WNetCancelConnection2A" (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 SubDirectoryName As String)

Dim myNetResource As New NETRESOURCE()

Dim errorText As String

If ServerShare.EndsWith("\") Then ServerShare =
ServerShare.Remove(ServerShare.Length - 1, 1)

myNetResource.dwScope = 2 'RESOURCE_GLOBALNET

myNetResource.dwType = 1 'RESOURCETYPE_DISK

myNetResource.dwDisplayType = 3 'RESOURCEDISPLAYTYPE_SHARE

myNetResource.dwUsage = 1 'RESOURCEUSAGE_CONNECTABLE

myNetResource.LocalName = Nothing 'Or use "P:" for mapped drive

myNetResource.RemoteName = ServerShare

myNetResource.Provider = Nothing

Dim ret As Integer = WNetAddConnection2(myNetResource, Password, User, 0)

If ret <> 0 Then

errorText = FormatErrorMessage(ret)

Throw New ApplicationException("Unable to connect network drive. Win32 error
code is: " & ret.ToString & " (" & errorText & ")")

End If

mstrDriveMappedTo = ServerShare

mstrUNCPath = ServerShare

If SubDirectoryName <> String.Empty Then

mstrDriveMappedTo &= "\" & SubDirectoryName

End If

End Sub

Public Sub Disconnect()

Dim CONNECT_UPDATE_PROFILE As Integer = 1

Dim FORCE_DRIVE_CLOSE As Integer = 1

Dim errorText As String

Dim ret As Integer = WNetCancelConnection2(mstrUNCPath,
CONNECT_UPDATE_PROFILE, FORCE_DRIVE_CLOSE)

If ret <> 0 Then

errorText = FormatErrorMessage(ret)

Throw New ApplicationException("Unable to disconnect network drive. Win32
error code is: " & ret.ToString & " (" & errorText & ")")

End If

mstrDriveMappedTo = String.Empty

End Sub

Public ReadOnly Property DriveMappedTo() As String

Get

Return mstrDriveMappedTo

End Get

End Property

Private Function FormatErrorMessage(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(FORMAT_MESSAGE_FROM_SYSTEM, 0, Win32ErrorCode, LANG_NEUTRAL,
buffer, 999, 0)

buffer = Replace(Replace(buffer, Chr(13), String.Empty), Chr(10),
String.Empty)

Return buffer.Substring(0, buffer.IndexOf(Chr(0)))

Catch

Return "Unable to determine error text"

End Try

End Function

<StructLayout(LayoutKind.Sequential)> _

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 said:
Hi,

I'd like to know how to easily map a network drive using vb.net.

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top