Having problems executing another executable from Visual Basic... Help!

Ameer27

New member
Joined
Sep 13, 2010
Messages
2
Programming Experience
Beginner
So, I'm trying to make a program that has command buttons, each running a different file. I have added some files so far, but I encountered a problem with a couple of them.

For example, I want to run Zuma's Revenge! right from the program, but it is not working. If I run the game from outside the program, it runs perfectly, but when I try running it from the program, it keeps saying "Zumas Revenge! - Adventure has stopped working."

What really confused me, is that the game is running normally if I run it from its folder or by using the shortcut, while from the program it isn't. Does anybody know what could be the problem?

I'm using Windows 7, with Visual Basic 2010 Express.

'Here's the code I'm using:

Private Sub Zumas_Revenge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Zumas_Revenge.Click
Process.Start(My.Application.Info.DirectoryPath & "\Zumas Revenge! - Adventure\ZumasRevengeAdventure.exe")
End Sub

What could be wrong?

Thanks in advance.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,212
Location
Sydney, Australia
Programming Experience
10+
When a program refers to a file without specifying an absolute path, it is assumed that the file is in the program's current directory. The current directory can be any directory at all and can change over the course of a session. If you have a look at the properties of the shortcuts on your Windows desktop or Start menu, you'll see that each of them has a Working Directory, which is the initial value for the current directory. Most shortcuts will specify the same folder as contains the EXE they run, but they don't have to. The thing is, most games REQUIRE that they do. In your case, you aren't specifying the working directory at all, so the current directory of your own program is used. When the game tries to find files in its current directory it fails, because it's looking in the wrong folder.

So, what you to do is specify the working directory for the new process explicitly. You do that using the ProcessStartInfo class:
Code:
Dim filePath As String = IO.Path.Combine(My.Application.Info.DirectoryPath,
                                         "Zumas Revenge! - Adventure\ZumasRevengeAdventure.exe")
Dim folderPath As String = IO.Path.GetDirectoryName(filePath)
Dim startInfo As New ProcessStartInfo(filePath)

startInfo.WorkingDirectory = folderPath
Process.Start(startInfo)
 
Top Bottom