|
|
Title | Run a DOS application and capture its output in VB .NET |
Description | This example shows how to run a DOS application and capture its output in VB .NET. |
Keywords | DOS, stdout, stderr, standard output, standard error, standard out, run, VB.NET |
Categories | Software Engineering, VB.NET, Strings, Files and Directories |
|
|
Create a ProcessStartInfo object. Use its properties to indicate that the DOS program should not use ShellExecute, should not create a separate window, and should redirect standard output and standard error.
Next create a Process object and set its StartInfo property to the ProcessStartInfo object. Then start the process.
Attach StreamReaders to the Process's StandardOutput and StandardError streams, read their contents, and display the results.
|
|
Private Sub btnRun_Click(...) Handles btnRun.Click
' Set start information.
Dim start_info As New ProcessStartInfo(txtProgram.Text)
start_info.UseShellExecute = False
start_info.CreateNoWindow = True
start_info.RedirectStandardOutput = True
start_info.RedirectStandardError = True
' Make the process and set its start information.
Dim proc As New Process()
proc.StartInfo = start_info
' Start the process.
proc.Start()
' Attach to stdout and stderr.
Dim std_out As StreamReader = proc.StandardOutput()
Dim std_err As StreamReader = proc.StandardError()
' Display the results.
txtStdout.Text = std_out.ReadToEnd()
txtStderr.Text = std_err.ReadToEnd()
' Clean up.
std_out.Close()
std_err.Close()
proc.Close()
End Sub
|
|
Note that this is a VB 2005 application. You should be able to use similar code in earlier versions of VB .NET.
For more information on programming in VB .NET, see my book Visual Basic 2005 Programmer's Reference.
|
|
|
|
|
|