473,804 Members | 2,164 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 5016
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

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 WNetAddConnecti on2 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 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


Nov 21 '05 #3

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

Similar topics

3
3657
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 my settings if my profile became tampered with or corrupt. Is there any sample code available out there? -Robert
2
5812
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 a while the network drive apears to have a red X on the icon and the drive becomes disconnected. this seems to effect the VB code from grabbing the tables. I get an error saying the specific table cannot be found. If i manually open the mapped...
5
21573
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 folder on mapped network drive (eg. N:\Storage that corresponds to \\FS1NS\SharedDir\), I get an error as "Could not find part of path N:\".
1
6575
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. TIA, Brian
8
11854
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, and use @"C:\file.txt"; it worked Thanks a lot
5
4478
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 seperate files server. we have mapped the fodler to a different folder and need to allow the EXE to process on the mapped drive. When I trigger the EXE via web service the EXE get the permission of the launching user (mean ASP.NET user) resulting a...
14
2327
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 .exe (on the network drive, on the server) "Full Trust." From the client, double-click on the ..exe (on the network drive, on the server) and it runs fine. So far, so good, but... On the server, where I've installed not VB.NET but .NET
2
2678
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 server where the network drive exists. Although of this check I am sometimes getting an exception (in 5% of times)saying "Could not find the U:\...\File.ext" or part of its path" Although the network dirve is available and accessible by windows...
3
2699
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, lsRecordType, lsXNbr, lsFiscYr, "Beg", CStr(H.BegBalAccDepn), CStr(H.BegBalCost), CStr(H.BegBalCostReval), CStr(H.BegBalDepCost), CStr(H.BegBalDepnReval)) The program is running from within a Virtual PC
10
11050
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 in C#. It doesnt seem like C$ would use MapNetworkDrive.
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7638
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6866
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
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 we have to send another system
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.