473,503 Members | 8,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How map a network drive

Hi,

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

thanks
Nov 21 '05 #1
2 4997
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" <Li****@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
Hi,

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

thanks

Nov 21 '05 #2
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" wrote:
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" <Li****@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
Hi,

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

thanks


Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3618
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
2
5767
by: giloosh99 | last post by:
Hello, Im grabbing tables via VB code using visual foxpro ODBC drives. The tables directory is in a mapped network drive. The code works fine and does the job, however if the computer is idle for...
5
21537
by: Niloday | last post by:
Hi All, I am trying to access a mapped network drive from a service that I have created. The service needs to create/delete folders/files on a network drive. When I tried to connect to a...
1
6552
by: brian.oneil2 | last post by:
Is there a way to install this onto a network file share and allow a team to access it? I would say share a CD from a networked CD drive, but there are multiple CD's that would have to be inserted....
8
11833
by: Lam | last post by:
HI anyone knows how can I open a mapped network file in C#? I try string file = @"T:\file.txt"; it shows me the error: "Could not find a part of the path" but if I copy the file to my C dirve,...
5
4447
by: Nirosh | last post by:
Hi All, Can any one suggest me a best way to do this .. I have a thrid party tool "EXE" that we need to use with our web service to manipulate some complex XML files, which reside in a...
14
2305
by: frostalicious | last post by:
Used VB.NET (on my client PC) to convert VB6 executable to .NET executable. Placed the .exe file on a network drive on my server. From client, ran .NET Wizards "Trust an Assembly" to make the...
2
2659
by: .Net Believer | last post by:
I using the routine below to copy file to a network drive for a regular backup process. Before calling this routine I using another function to check the presence of the LAN connection and the...
3
2678
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
10
10975
by: =?Utf-8?B?Z3JlYXRiYXJyaWVyODY=?= | last post by:
Sorry about that previous one. I pressed enter too early. How does one go about mapping a network drive in C#. i know you use MapNetworkDrive in scripting languages, but i'm not sure how to do it...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.