Today we will see how to launch external applications from within VB.NET application and control its input and output. For this demo, we will make our own Command Prompt – a form from where you can execute DOS commands and get the result too.
How to control an external program from within our VB.NET application, send input to it and get output from it.
Start a new application (or add a form to your application).
Add two textboxes and a button to it.
Set the following properties:
Name = OutputTextBox
Multiline = True
ReadOnly = True
Scrollbars = Both
Name = InputTextBox
TabIndex = 0
Name = ExecuteButton
Text = Execute
Arrange the controls on the form appropriately. See screenshot below:
Open the code window and add the following code:
Private WithEvents MyProcess As Process Private Delegate Sub AppendOutputTextDelegate(ByVal text As String) Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.AcceptButton = ExecuteButton MyProcess = New Process With MyProcess.StartInfo .FileName = "CMD.EXE" .UseShellExecute = False .CreateNoWindow = True .RedirectStandardInput = True .RedirectStandardOutput = True .RedirectStandardError = True End With MyProcess.Start() MyProcess.BeginErrorReadLine() MyProcess.BeginOutputReadLine() AppendOutputText("Process Started at: " & MyProcess.StartTime.ToString) End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing MyProcess.StandardInput.WriteLine("EXIT") 'send an EXIT command to the Command Prompt MyProcess.StandardInput.Flush() MyProcess.Close() End Sub Private Sub MyProcess_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.ErrorDataReceived AppendOutputText(vbCrLf & "Error: " & e.Data) End Sub Private Sub MyProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.OutputDataReceived AppendOutputText(vbCrLf & e.Data) End Sub Private Sub ExecuteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExecuteButton.Click MyProcess.StandardInput.WriteLine(InputTextBox.Text) MyProcess.StandardInput.Flush() InputTextBox.Text = "" End Sub Private Sub AppendOutputText(ByVal text As String) If OutputTextBox.InvokeRequired Then Dim myDelegate As New AppendOutputTextDelegate(AddressOf AppendOutputText) Me.Invoke(myDelegate, text) Else OutputTextBox.AppendText(text) End If End Sub
Run the code. Enter any DOS command in the InputTextBox and press enter (or click Execute button). The output should appear in the OutputTextBox.
If you are not familiar with DOS, type HELP and it should show you the list of all commands it recognizes.
How & Why this Works
MyProcess is declared WithEvents so that we can trap the events it raises. Alternatively you may put a Process control to your form.
Setting the CreateNoWindow property instructs it that we do not want it to show any window (in this case, the black command prompt window it would normally show).
We redirect the standard Input, Output and Error streams so that we can handle them ourself. We provide it the input from our InputTextBox, and get the output/error in our OutputTextBox.
This technique would only work for applications that use the standard input/output (StdIn/StdOut/StdErr).
There is no error handling added here for the simplicity of demo. You should always add appropriate error handlers where there is a chance of error occurring.
Beware that some DOS commands can be harmful to your system. e.g. If you execute a FORMAT / DEL etc. commands, the deleted files would be unrecoverable.
Execute DOS commands at your own risk. I take no responsibility for any damage caused to your system whatsoever.
EDIT: 31-may-2012 — Applied syntax highlighter tags to code