Discussion Locked

Executing DOS command in Visual Basic?

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,

Im trying to execute an external DOS command from my VB code, when the initial form loads.

The command is a simple EXE.

What's the best way to code this!?

Thank you!

Mike@IHS

5 replies

by vinnyd

To run a dos command from VB you would use the Shell command.

Shell "command.com /c dir *.*"

The example about finds the command.com file and runs the internal dir command.

To run a program that does not have a path to it you must specify the full path name when you run it.

Shell("C:\WINDOWS\CALC.EXE")

Runs the Calculator.

by r. a. caluste

Answer 1 is right. You can also specify the window style in your shell command.

The Shell syntax:

Public Function Shell( _
ByVal Pathname As String, _
Optional ByVal Style As AppWinStyle = AppWinStyle.MinimizedFocus, _
Optional ByVal Wait As Boolean = False, _
Optional ByVal Timeout As Integer = -1 _
) As Integer

Optional window styles:

1. AppWinStyle.Hide = The window is hidden and focus is given to the hidden window.
2. AppWinStyle.NormalFocus = The window isgiven focus and displayed in its most recent size and position.
3. AppWinStyle.MinimizedFocus = The window is given focus and displayed as an icon.
4. AppWinStyle.MaximizedFocus = The window is given focus and displayed using the entire screen.
5. AppWinStyle.NormalNoFocus = The window is set to its most recent size and position. The currently active window remains in focus.
6. AppWinStyle.MinimizedNoFocus = The window is displayed as an icon. The currently active window remains in focus.

Example:

Shell("C:\WINDOWS\CALC.EXE", AppWinStyle.NormalFocus)

The above example opens the calculator in normal size and gives it the folcus.

This discussion is closed to new replies.