Solved

Mapping a network drive

Posted on 2002-07-10
12
191 Views
Last Modified: 2008-03-10
Experts,

I'm a total rookie at VB som please bare with me. I have been given the task of making a vb-exe-file that maps up three different network-shares depending on commandbutton-clicks.

I.e. I want to execute the following system-command at the click of a button:

net use u: /d > NUL
net use u: \\myshare\myfolder
Can anyone help me...?

HenningF
0
Comment
Question by:HenningF
  • 3
  • 3
  • 2
  • +4
12 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
Don't know if this link make help:

http://www.mvps.org/vbnet/code/network/netshareadd.htm
0
 
LVL 1

Author Comment

by:HenningF
Comment Utility
Unfortunatley I'm not experienced enaugh to understand if the link you gave me would do the trick or not. But something tells me that running two lines of system command shouldn't be all that hard, and the two lines are always the same so it needs no user-interface or nothing, just the click of a button. It will then map three different shares to the same drive-letter depending on which button is pushed.

If I have explained poorly please let me know...

HenningF
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
what ryancys gave you is the API way of doing things, which is the "proper" way to do things.

The "quick and dirty" way would be:
shell "net use u: /d > NUL" & vbcrlf & "net use u: \\myshare\myfolder"

Or, if that gives problems, save the lines to a .bat file and run that batch file using the Shell function.

CHeers
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
Bare in mind that the Shell function as such is async, so when your code continues, the NET command might not have finished, and you won't get reported any problems. And that's the reason why you would use the API, which would return an error value to your application.

CHeers
0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
I don't know if the above link is what you want.  It sounds like you want to connect to a share, not create one.  Here is the link you would want...

http://www.mvps.org/vbnet/index.html?code/network/netshareadd.htm

Or...

You can use the quick and dirty Shell function to execute the net use command.  Do the following...

Create a new project.
Place a command button on the form named Command1.
Paste the following code into the General declarations section of the form...

Private Sub Command1_Click()
   
   Shell "net use u: /d > NUL"
   Shell "net use u: \\SERVER\SHARE"
   
End Sub

Change the name to your path and drive letter.  This should work.
0
 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
This will use shell to issue the "net use" command to delete the mapping and then it will wait for the command to complete before attempting to re-map the drive.

Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Const STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400

Private Sub Shell32Bit(ByVal JobToDo As String)
         Dim hProcess As Long
         Dim RetVal As Long
         hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(JobToDo, vbHide))
         Do
             GetExitCodeProcess hProcess, RetVal
             DoEvents: Sleep 100
         Loop While RetVal = STILL_ACTIVE
End Sub

Private Sub Command1_Click()
' Delete mapping and wait for it to complete before remapping
Shell32Bit "command.com /c net use u: /d > NUL"
' This will not run until above command is completed
Shell32Bit "command.com /c net use u: \\myshare\myfolder"
End Sub
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
WshNetwork.MapNetworkDrive
The MapNetworkDrive method maps the share point specified by strRemoteName to the local resource name, strLocalName.

Syntax
WshNetwork.MapNetworkDrive strLocalName, strRemoteName, [bUpdateProfile], [strUser], [strPassword]
 
Parameters
strLocalName
Local resource name to map to.
strRemoteName
Remote share to map.
bUpdateProfile
If bUpdateProfile is supplied and its value is TRUE, this mapping is stored in the user profile.
strUser
If you are mapping the share point using the credentials of someone other than current user, you can specify strUser and strPassword.
strPassword
If you are mapping the share point using the credentials of someone other than current user, you can specify strUser and strPassword.
Example
Set WshNetwork = Wscript.CreateObject("Wscript.Network")
WshNetwork.MapNetworkDrive "Z:", "\\Server\Share"

 
0
 
LVL 1

Author Comment

by:HenningF
Comment Utility
Thanx guys...I'm trying out these solutions as we speak, I will get back to you shortly...

HenningF
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
Take in mind that you need to have an error handler in the mapping procedure.
0
 
LVL 5

Accepted Solution

by:
jayeshshah earned 150 total points
Comment Utility
Hi there,
You can use the below code to map a network drive
=====================================================
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 = "K:"                  'Local drive letter to be mapped
    strPassword = ""                            'specify network password if required
    strNetworkPathName = "\\NETWORKPATH\VOL1"   '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

=========================================================

Hope this sufficies your work

0
 
LVL 1

Author Comment

by:HenningF
Comment Utility
Yeehaa! Finally a soultion I got to work on the fly! I needed to add another function to remove the mapping, but once you got me on to the WNetAddConnection I searched MSDN Online and found how to do that aswell.

I increased the points to 150 for the excellent answer :) I would have given some points to all you other guys aswell, but unfortunatly I don't have many left... sorry.

HenningF
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
"Yeehaa! Finally a soultion I got to work on the fly!
.....
....."
Why?, code from MSDN didn't works?. I tried/used it whitout problems...!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now