News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

Run a DOS application and capture its output in VB .NET

Started by nandagopal, Nov 06, 2008, 06:55 PM

Previous topic - Next topic

nandagopal

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