This is a sample wrapper around cmd.exe that demonstrates how to capture standard output in realtime.


vb Code:
  1. Imports System.Diagnostics
  2.  
  3. Public Class frmConsoleDemo
  4.  
  5.     Friend WithEvents txtConsoleOut As System.Windows.Forms.TextBox
  6.     Friend WithEvents txtConsoleIn As System.Windows.Forms.TextBox
  7.  
  8.     Private psi As ProcessStartInfo
  9.     Private cmd As Process
  10.     Private Delegate Sub InvokeWithString(ByVal text As String)
  11.  
  12.     Private Sub frmConsoleDemo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  13.  
  14.         psi = New ProcessStartInfo("cmd.exe")
  15.  
  16.         Dim systemencoding As System.Text.Encoding = _
  17.             System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
  18.  
  19.         With psi
  20.             .UseShellExecute = False  ' Required for redirection
  21.             .RedirectStandardError = True
  22.             .RedirectStandardOutput = True
  23.             .RedirectStandardInput = True
  24.             .CreateNoWindow = True
  25.             .StandardOutputEncoding = systemencoding  ' Use OEM encoding for console applications
  26.             .StandardErrorEncoding = systemencoding
  27.         End With
  28.  
  29.         ' EnableraisingEvents is required for Exited event
  30.         cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True}
  31.  
  32.         AddHandler cmd.ErrorDataReceived, AddressOf Async_Data_Received
  33.         AddHandler cmd.OutputDataReceived, AddressOf Async_Data_Received
  34.         AddHandler cmd.Exited, AddressOf CMD_Exited
  35.  
  36.         cmd.Start()
  37.         ' Start async reading of the redirected streams
  38.         ' Without these calls the events won't fire
  39.         cmd.BeginOutputReadLine()
  40.         cmd.BeginErrorReadLine()
  41.  
  42.         Me.txtConsoleIn.Select()
  43.     End Sub
  44.  
  45.     Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
  46.         Me.Close()
  47.     End Sub
  48.  
  49.     ' This sub gets called in a different thread so invokation is required
  50.     Private Sub Async_Data_Received(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
  51.         Me.Invoke(New InvokeWithString(AddressOf Sync_Output), e.Data)
  52.     End Sub
  53.  
  54.     Private Sub Sync_Output(ByVal text As String)
  55.         txtConsoleOut.AppendText(text & Environment.NewLine)
  56.         txtConsoleOut.ScrollToCaret()
  57.     End Sub
  58.  
  59.     ' Sending console commands here
  60.     Private Sub txtConsoleIn_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtConsoleIn.KeyPress
  61.         If e.KeyChar = ControlChars.Cr Then
  62.             cmd.StandardInput.WriteLine(txtConsoleIn.Text)
  63.             txtConsoleIn.Clear()
  64.         End If
  65.     End Sub
  66.  
  67.     ' Two text boxes called txtConsoleOut and txtConsoleIn
  68.     Public Sub New()
  69.         Me.txtConsoleOut = New System.Windows.Forms.TextBox()
  70.         Me.txtConsoleIn = New System.Windows.Forms.TextBox()
  71.         Me.SuspendLayout()
  72.         '
  73.         'txtConsoleOut
  74.         '
  75.         Me.txtConsoleOut.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  76.                     Or System.Windows.Forms.AnchorStyles.Left) _
  77.                     Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
  78.         Me.txtConsoleOut.Location = New System.Drawing.Point(12, 12)
  79.         Me.txtConsoleOut.Multiline = True
  80.         Me.txtConsoleOut.Name = "txtConsoleOut"
  81.         Me.txtConsoleOut.ReadOnly = True
  82.         Me.txtConsoleOut.ScrollBars = System.Windows.Forms.ScrollBars.Both
  83.         Me.txtConsoleOut.Size = New System.Drawing.Size(665, 494)
  84.         Me.txtConsoleOut.TabIndex = 0
  85.         '
  86.         'txtConsoleIn
  87.         '
  88.         Me.txtConsoleIn.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
  89.                     Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
  90.         Me.txtConsoleIn.Location = New System.Drawing.Point(10, 512)
  91.         Me.txtConsoleIn.Name = "txtConsoleIn"
  92.         Me.txtConsoleIn.Size = New System.Drawing.Size(667, 20)
  93.         Me.txtConsoleIn.TabIndex = 1
  94.         '
  95.         'frmConsoleDemo
  96.         '
  97.         Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
  98.         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
  99.         Me.ClientSize = New System.Drawing.Size(689, 544)
  100.         Me.Controls.Add(Me.txtConsoleIn)
  101.         Me.Controls.Add(Me.txtConsoleOut)
  102.         Me.Name = "frmConsoleDemo"
  103.         Me.Text = "Console redirect demo"
  104.         Me.ResumeLayout(False)
  105.         Me.PerformLayout()
  106.  
  107.     End Sub
  108.  
  109. End Class