• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1888
  • Last Modified:

Connect to a Network Drive and open a file using Visual Basic

Hello:

I am creating a Visual Basic 5.0 application and I placed a command control button on a form. Behind the command button, I need to put in code that will do the following:

Look at a Network Drive and launch an .exe(ex: dan.exe) file no matter where it is located on the drive and run it ontop of the form.

Is this possible?

If so, can you give me some examples of code on how I can do this?

Thanx,

Dan M
0
dmfish1968
Asked:
dmfish1968
2 Solutions
 
jay_s5Commented:
Hi Dan,

There are basically two ways to run an application from a VB app. Take a look at this KB article for details and sample code for each:

http://support.microsoft.com/kb/129797

You would simply add code to substitute the name of your executable (dan.exe) where they used calc.exe in the examples.

I need more specifics on what you are doing with the network drive to help with that part.
0
 
Johnny NewbeeCommented:
'Step 1: Map the target network drive if you haven't map yet like "H:/" on your computer. Else", just skip to step 2 and put in "H:/" as the target drive.

'Find and check the Windows Script Host object from Project - Reference

'code this under your command button

Private Sub Command1_Click()

Dim onet As New WshNetwork
Dim strTargetdrive as String

On Error GoTo Command1_Click_Error

strTargetdrive="H:"

onet.MapNetworkDrive strTargetdrive, "\\" & servername & "\" & sharename
'replace with the your targer servername and the sharename; you may name other letter than H, but make sure no other drive using the same

'If the user is not logged on to the network you will have to do this:
onet.MapNetworkDrive strTargetdrive, "\\" & servername & "\" & sharename, , DomainName\UserName, Password

'call step 2

'call step 3

Exit Sub

Command1_Click_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Command1_Click of Form Form1"

End Sub


'Step 2: Search the network drive for the target file and get its path

'You need to select the Microsoft Scripting Runtime from Reference. But I don't recall VB 5 has it. So you either go for VB 6 or use the API Call as explained from the link:

http://support.microsoft.com/kb/185476/EN-US/

'You need to return the target path from the search function againsting the target exe file

'Step 3: fire up the exe file

Dim strTarget As String
strTarget =  thetargetpathfromstep2 & targetexefilename
Call ShellExecute(0, "Open", strTarget, "", "", 1)

'at the end, you could even check if the Shelled process has terminated or not from the sample code at this link:
http://support.microsoft.com/kb/96844/EN-US/


HTH

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now