How to Run an EXE File in VB Code

Written by ed stanyer
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Run an EXE File in VB Code
VB.Net uses the 'shell' function to run external exe files. (Stockbyte/Stockbyte/Getty Images)

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

Other People Are Reading

Things you need

  • VB.Net 2005 / 2008 / 2010

Show MoreHide


  1. 1

    Load the Visual Studio development program from the "Start" menu.

  2. 2

    Create a new Windows Forms project, give it a useful name and choose an appropriate file location.

  3. 3

    Create a new button in form 1 and call it "cmdshell."

  4. 4

    Double-click on the newly created "cmdshell" button to bring up the code window for it.

  5. 5

    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)"

  6. 6

    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:
  • "Try
  • Shell("c:\windows\notepad.exe" , AppWinStyle.NormalFocus)
  • Catch
  • MessageBox.Show ("Unable to locate the desired file")
  • End Try"

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.