title: Vb.NET Start Process and return output
body: <p>I'm trying to execute some command line commands (putty). Now it may be that the command line waits for an input (simplest example: password maybe wrong). All the output should be written into Console.</p>
  
  <p>My code displays a non-ending command line that does not write anything to the console.</p>
  
  <pre><code>Imports System.Text
  
  Public Class Form1
      Private Shared processOutput As StringBuilder = Nothing
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          Dim line As String = "/c plink -ssh chef@192.168.178.35 -pw 12345678 -m C:\putty\pw\putty.txt"
          processOutput = New StringBuilder()
          Dim objP As New System.Diagnostics.Process()
          Dim objPi As ProcessStartInfo = New ProcessStartInfo()
          With objPi
              .FileName = "cmd.exe"
              .Arguments = line
              .RedirectStandardOutput = True
              .RedirectStandardError = True
              .RedirectStandardInput = True
              .UseShellExecute = False
              .WindowStyle = ProcessWindowStyle.Hidden
              .CreateNoWindow = False
          End With
          objP.StartInfo = objPi
  
          AddHandler objP.OutputDataReceived, AddressOf OutputHandler
          objP.Start()
          objP.BeginOutputReadLine()
          objP.WaitForExit(1000)
          Debug.WriteLine(processOutput.ToString())
      End Sub
  
      Private Shared Sub OutputHandler(sendingProcess As Object, outLine As DataReceivedEventArgs)
          If Not String.IsNullOrEmpty(outLine.Data) Then
              processOutput.AppendLine(outLine.Data)
          End If
      End Sub
  
  
  End Class
  </code></pre>