How to Open a Program From Another VB.Net Code
VB.Net is Microsoft's version of the Visual Basic programming language for the .NET framework and the "Express" version is available free from Microsoft's website as part of their Visual Studio suite. In previous versions of Visual Basic, the "shell" command would be used to open a program from within your code, but this had the problem that the code would continue running without waiting for the launched program to exit. In VB.net, a new System.Diagnostics namespace is available, allowing you to launch programs as processes that can be monitored from within your code.
Instructions
-
-
1
Start Visual Basic.Net by clicking the Windows "Start" button and clicking "All Programs." Open the "Microsoft Visual Express" folder and then then click "Microsoft Visual Basic Express" to launch the application.
-
2
Create a new project by selecting "New Project" from the "File" menu. When prompted for the application type, double-click "Windows Form Application."
-
-
3
Drag a button component from the toolbox on the left side of the screen onto the blank form that has been automatically created by the Visual Studio editor.
-
4
Double-click on the button component to add the code that will run when the button is clicked.
-
5
Add the code below to the body of the sub routine automatically created by the code editor:
Dim myProcessSetup As New System.Diagnostics.ProcessStartInfo("notepad.exe")
myProcessSetup.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
Dim myProcess As Process = System.Diagnostics.Process.Start(myProcessSetup)
First, a new process object is created named "myProcessSetup" which will open "notepad.exe" when the process is started. The "WindowStyle" property is then set for the newly created process. Finally, the process is launched with the "Start" command.
-
6
Press "F5" to launch the application. Click on the button and you should see the "Notepad" application start automatically.
-
1
Tips & Warnings
Adding the following line after the code to start the process to make the rest of the code wait until the process has been closed before continuing:
myProcess.WaitForExit()