VB.Net offers a function that allows the execution of exe files within the program. An exe file can be loaded directly into focus or in the background. For this function to work you must know the absolute path of the exe file. The function will return an error if the file is not found and this allows for error handling to be put in place to deal with the error accordingly without having to end the program.
- Skill level:
- Moderately Easy
Things you need
- VB.Net 2005 / 2008 / 2010
Load the Visual Studio development program from the "Start" menu.
Create a new Windows Forms project, give it a useful name and choose an appropriate file location.
Create a new button in form 1 and call it "cmdshell."
Double-click on the newly created "cmdshell" button to bring up the code window for it.
Use the "shell" function to run an exe file; you must enclose the exact path to the exe file in double quotes and brackets. An example of the code would be as follows:
"Shell("c:\windows\notepad.exe" , AppWinStyle.NormalFocus)"
Run the program by pressing F5 and click the button on the form to load the exe file specified in the code.
Tips and warnings
- The "AppWinStyle.NormalFocus" code ensures that the exe file runs in main focus. If you wish to run the program in the background, use the "AppWinStyle.NormalNoFocus" code instead. To add error handling to your shell function, use the "Try" and "Catch" functions to ensure the program does not crash if a file is not found. Example code would be:
- Shell("c:\windows\notepad.exe" , AppWinStyle.NormalFocus)
- MessageBox.Show ("Unable to locate the desired file")
- End Try"
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for