Friday, April 23, 2010

Opening Another Program in VB.NET

Hey I got this code from the website www.dreamincode.net/forums


Try
Dim p As New System.Diagnostics.Process
'the location of your program
p.StartInfo.FileName = "K:\Uni\Year 2\Visual Basic\Report\MSDN\Absolute Beginner's Series VB Lesson 1\01 VB Code\Lesson01\HelloWorld.exe" 
p.Start()
Catch ex As Exception
MsgBox("Error - " + ex.Message)
End Try

This can be useful in opening other applications (especially Microsoft VB.NET applications). The problems is that there are chances of error especially when the program is missing, or the program has been moved.


As a solution to that problem, I suggest that you would make the separate application relative to your executable file so that
moving the whole project to another location whould not affect the path you specified.
Your file path should look like this.
p.StartInfo.FileName = "Your Program.exe"  'your relative path, no need to include the drive location for portability

Just make sure that the program is located beside the program you are debugging.
You can do this by adding another project as a reference to your main project so that everytime you compile, the executables automatically
proceed to the bin where they would reside as they are executed.
To add a refecence, you can follow these steps:

If the other program (VB Project) is not yet part of the Solution:
1. go to File > Add Existing project
2. Then select the project file of the program you want to link it with.
You will notice in the solution explorer that another project is added.

Now, you can make that program part of the main program by adding it as a reference.
On your solution explorer, right click your main project, Choose Add reference.

As the add reference dialog opens, choose projects then select the project file of the other program.

Add Reference
Pro VB 2008 and the .NET 3.5 Platform (Windows.Net)
Pro VB 2008 and the .NET 3.5 Platform (Windows.Net)

No comments:

Post a Comment