|
-
Feb 21st, 2003, 03:52 PM
#1
Thread Starter
PowerPoster
VB Snippet - Map a Drive
VB Code:
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
Dim strLocalDriveLetter As String
Dim strPassword As String
Dim strNetworkPathName As String
strLocalDriveLetter = "Z:" 'Local drive letter to be mapped
strPassword = "" 'specify network password if required
strNetworkPathName = "\\NEWPATH\NEWPATH" 'path to network drive
If WNetAddConnection(strNetworkPathName, strPassword, strLocalDriveLetter) > 0 Then
MsgBox ("An Error occurred mapping the drive")
Else
MsgBox ("Drive successfully mapped!")
End If
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Mar 6th, 2003, 04:03 PM
#2
Member
nice! How would you modify this to prompt for a network drive to map? Like another server or share. If you hard code it, looks like you need to have all the servers the same name or create several instances
-
Mar 12th, 2003, 06:34 PM
#3
Fanatic Member
Use a bunch of inputboxes or a custom form.
-
Mar 12th, 2003, 07:40 PM
#4
Member
sometimes I wonder about myself
-
Apr 2nd, 2004, 10:42 AM
#5
Addicted Member
another way
another possible way (without api's) is through SHELL command 'subst'. Here is the help from the terminal:
Code:
H:\>help subst
Associates a path with a drive letter.
SUBST [drive1: [drive2:]path]
SUBST drive1: /D
drive1: Specifies a virtual drive to which you want to assign a path.
[drive2:]path Specifies a physical drive and path you want to assign to
a virtual drive.
/D Deletes a substituted (virtual) drive.
Type SUBST with no parameters to display a list of current virtual drives.
And here is how you can use it:
VB Code:
' map the windows folder to the S drive
Shell("subst c:\windows s:"
' remove the map
Shell("subst s: \D")
This might be easier if you want to do it on one line...
-
Apr 17th, 2004, 04:09 PM
#6
Even another way - no vb.
Create a bat file to map a drive.
You could always shell out the .bat file in vb if you wanted.
Code:
@echo off
echo Mapping Drive.......
NET USE R: \\ServerName\ShareName /PERSISTENT:YES
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 27th, 2006, 06:06 AM
#7
Member
Re: VB Snippet - Map a Drive
Question: When I use the "NET USE" code if doesn't remove the drive icon. What can I do about? An yes, I hit F5 several times. When I click the icon a error message shows.
-
Feb 27th, 2006, 08:10 AM
#8
Re: VB Snippet - Map a Drive
 Originally Posted by nbmprivat
Question: When I use the "NET USE" code if doesn't remove the drive icon. What can I do about? An yes, I hit F5 several times. When I click the icon a error message shows.
So, it disconnects the drive but leaves the icon in place?
-
Feb 27th, 2006, 08:14 AM
#9
Member
Re: VB Snippet - Map a Drive
 Originally Posted by Hack
So, it disconnects the drive but leaves the icon in place?
Yes...
-
Feb 27th, 2006, 08:17 AM
#10
Re: VB Snippet - Map a Drive
Have you tried using the API instead of NET USE?
-
Feb 27th, 2006, 08:18 AM
#11
Member
Re: VB Snippet - Map a Drive
Nope... but I dropped the drivemapping solution. I just use the direct network path to start batch files.
-
Feb 27th, 2006, 08:25 AM
#12
Re: VB Snippet - Map a Drive
 Originally Posted by RobDog888
Even another way - no vb.
Create a bat file to map a drive.
You could always shell out the .bat file in vb if you wanted.
Code:
@echo off
echo Mapping Drive.......
NET USE R: \\ServerName\ShareName /PERSISTENT:YES
Hey I saw that today because of a new post.
If you again don't want to Hardcode the values here is an extension of RD's code
Code:
@echo off
echo Mapping Drive.......
NET USE %1 %2 /PERSISTENT:YES
If you save this file as MAPDRIVE.BAt then you can execute it by passing the drive and path like this
Code:
MAPDRIVE P: \\ServerName\ShareName
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Apr 5th, 2006, 08:35 AM
#13
Frenzied Member
Re: VB Snippet - Map a Drive
strNetworkPathName = "\\NEWPATH\NEWPATH" 'path to network drive
the PC is on the network however its is under a different domain so doesnt pick it up, how do i modify strNetworkPathName = "\\NEWPATH\NEWPATH" if under different domain
cheers
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Apr 5th, 2006, 09:05 AM
#14
Frenzied Member
Re: VB Snippet - Map a Drive
 Originally Posted by James Stanich
VB Code:
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
Dim strLocalDriveLetter As String
Dim strPassword As String
Dim strNetworkPathName As String
strLocalDriveLetter = "Z:" 'Local drive letter to be mapped
strPassword = "" 'specify network password if required
strNetworkPathName = "\\NEWPATH\NEWPATH" 'path to network drive
If WNetAddConnection(strNetworkPathName, strPassword, strLocalDriveLetter) > 0 Then
MsgBox ("An Error occurred mapping the drive")
Else
MsgBox ("Drive successfully mapped!")
End If
oh also how do u disconnect afterwards?
Last edited by Robbo; Apr 5th, 2006 at 09:12 AM.
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Apr 5th, 2006, 10:22 AM
#15
Re: VB Snippet - Map a Drive
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 5th, 2006, 10:48 AM
#16
Frenzied Member
Re: VB Snippet - Map a Drive
 Originally Posted by RobDog888
that using this method??
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
Dim strLocalDriveLetter As String
Dim strPassword As String
Dim strNetworkPathName As String
strLocalDriveLetter = "Z:" 'Local drive letter to be mapped
strPassword = "" 'specify network password if required
strNetworkPathName = "\\NEWPATH\NEWPATH" 'path to network drive
If WNetAddConnection(strNetworkPathName, strPassword, strLocalDriveLetter) > 0 Then
MsgBox ("An Error occurred mapping the drive")
Else
MsgBox ("Drive successfully mapped!")
End If
??
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Apr 5th, 2006, 10:51 AM
#17
Re: VB Snippet - Map a Drive
No, the menu didnt sync so when I copied the url it was on another api I had lloked at. This one is the one - http://windowssdk.msdn.microsoft.com...asp?frame=true
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 5th, 2006, 11:01 AM
#18
Frenzied Member
Re: VB Snippet - Map a Drive
 Originally Posted by RobDog888
still not sure what string is? just copy and paste if you like?
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Apr 5th, 2006, 11:11 AM
#19
Re: VB Snippet - Map a Drive
I think this is the correct conversion from C++ to VB6.
VB Code:
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, _
ByVal dwFlags As Long, ByVal lpszLocalName As Boolean) As Long
Here is the C++ code example of its use.
http://windowssdk.msdn.microsoft.com...connection.asp
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 6th, 2006, 06:12 AM
#20
Frenzied Member
Re: VB Snippet - Map a Drive
 Originally Posted by RobDog888
I think this is the correct conversion from C++ to VB6.
VB Code:
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, _
ByVal dwFlags As Long, ByVal lpszLocalName As Boolean) As Long
Here is the C++ code example of its use.
http://windowssdk.msdn.microsoft.com...connection.asp
ok
well have theses two
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
so should i replace second with this?
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal lpszLocalName As Boolean) As Long
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Apr 6th, 2006, 08:51 AM
#21
Re: VB Snippet - Map a Drive
Did you look at the example in the link I posted?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 7th, 2006, 04:58 AM
#22
Frenzied Member
Re: VB Snippet - Map a Drive
 Originally Posted by RobDog888
Did you look at the example in the link I posted?
ok thats in C++, will have a try see if can get it working if not will post the code cheers
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Apr 7th, 2006, 05:06 AM
#23
Frenzied Member
Re: VB Snippet - Map a Drive
ok
Private Sub UNMAP_DRIVE()
If WNetCancelConnection2(strNetworkPathName, strPassword, strLocalDriveLetter) = 0 Then
MsgBox ("Mapped drive removed")
End
Else
MsgBox (" not successfully unmapped!")
End If
End Sub
not picking it up, what does WNetCancelConnection2 have to equal or should be?
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Apr 7th, 2006, 08:07 AM
#24
Re: VB Snippet - Map a Drive
Check the "Return Values" section of the api definition.
http://windowssdk.msdn.microsoft.com...onnection2.asp
I also think you have your paramaters wrong.
Should be - "Drive letter", CONNECT_UPDATE_PROFILE, False
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 7th, 2006, 08:44 AM
#25
Frenzied Member
Re: VB Snippet - Map a Drive
will have another mess see if i can get it to work, never used this command so like looking for a drawing pin in the dark, not sure as theres no help on it in VB
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Apr 7th, 2006, 08:47 AM
#26
Re: VB Snippet - Map a Drive
There shouldnt be that much to it...
VB Code:
If WNetCancelConnection2("Drive letter", CONNECT_UPDATE_PROFILE, False) = NO_ERROR Then
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 7th, 2006, 08:47 AM
#27
Frenzied Member
Re: VB Snippet - Map a Drive
yippy
this worked
Private Sub UNMAP_DRIVE()
If WNetCancelConnection2(strLocalDriveLetter, CONNECT_UPDATE_PROFILE, False) = 0 Then
'MsgBox ("Mapped drive removed")
End
Else
MsgBox ("Not successfully unmapped!")
End If
End Sub
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Apr 7th, 2006, 08:49 AM
#28
Re: VB Snippet - Map a Drive
Therre you go See isnt it more saisifying when you only get guidence and not everything handed to you on a silver platter. Now you will not forget this each time you need to map/unmap a drive. 
Note the False argument is not to force a disconnection if there is an open job or process using the resource.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|