title: VB.Net process exits as soon as I attempt to read standard output or error
body: <h2>Initial Problem</h2>
  
  <p>I apologize if this issue has been raised and addressed elsewhere; I searched this site, and Google at large without any luck.</p>
  
  <p>I'm trying to write a simple VB.Net Windows Forms Application to allow a user to run the Windows File Compare program (fc.exe) with a very simple GUI ("browse" buttons to select files, checkboxes to select modifiers, and a textbox for the output).</p>
  
  <p>The problem is that whenever I try to read the standard output or error from the process, it immediately stops, and nothing is output.  I've verified that the process arguments are correct by setting "createnowindow" to False and not redirecting Output or Errors.</p>
  
  <p>To see if the process is actually running or not, I put a "while" loop after proc.start:</p>
  
  <pre><code>Do While proc.HasExited = False
  textbox.AppendText(i &amp; vbNewLine)
  i += 1
  Loop
  </code></pre>
  
  <p>If the process runs normally, I get a count up to about 80 or 90.  If I do anything at all with the standardoutput or standarderror, the textbox only shows the initial value of "0".  By "anything at all", I mean assigning the <code>proc.StandardOutput.ReadToEnd</code> to a variable.  If I use <code>proc.StandardOutput.Peek</code>, it returns a <code>-1</code> and the loop remains at 0.</p>
  
  <p>I've noticed that if I only redirect either Output <em>or</em> Error (but not both), and I enable the process to open a new window, the new window is empty and immediately exits (even if I'm not attempting to read the redirected stream in my code), whereas if neither is redirected, it displays a few pages of results, then exits.  I don't know if this is normal, or if the File Compare executable is somehow mixing the Output and Error streams to generate its output, or if something like that is even possible.</p>
  
  <p>I'm extremely new to coding in general (I've been working with VB.net for about a month, and that's the extent of my programming experience), so my I'm aware that my troubleshooting and assumptions may be completely off base, and I appreciate any assistance anyone can provide.  As it is, I'm completely floundering, and my inexperience is making it difficult to look for alternatives (for instance, I can't figure out how to correctly handle asynchronous output).  For reference, here's my embarrassingly clunky code as it currently stands:</p>
  
  <pre><code>Dim cmdinput As String = """" &amp; file1path &amp; """" &amp; " " &amp; """" &amp; file2path &amp; """"
      Dim cmdmods As String = " "
      Dim i As Integer = 0
      Dim proc As New Process
      proc.StartInfo.CreateNoWindow = True
      proc.StartInfo.UseShellExecute = False
      proc.StartInfo.FileName = "C:\Windows\System32\fc.exe"
      proc.StartInfo.Arguments = cmdinput &amp; cmdmods
      proc.StartInfo.RedirectStandardOutput = True
      proc.StartInfo.RedirectStandardError = True
      proc.Start()
      proc.StandardOutput.ReadToEnd()
  
      Do While proc.HasExited = False
          scanbox.AppendText(i &amp; vbNewLine)
  
          i += 1
      Loop
  </code></pre>
  
  <h2>Possible Solution</h2>
  
  <p>After Hans Passant pointed out that I should be seeing errors, if nothing else, I messed around with my code and was able to get a result, though a less than optimal one.  Instead of running FC.exe directly, I ran CMD.exe.  I had tried this before with no luck, but that's because CMD.exe doesn't accept "fc " as process.startinfo.arguments.</p>
  
  <p>I passed the "fc " to cmd.exe with proc.standardinput.writeline().  At this point I was able to read CMD.exe's redirected output.  I still have no idea why I can't directly read FC.exe, but this is a pretty good band-aid in the meantime.  On the off chance that anyone else feels the need to add a GUI to a perfectly good command-line executable and runs into problems, here's my code:</p>
  
  <pre><code>Public Sub compare()
      Dim cmdinput As String = "fc " &amp; """" &amp; file1path &amp; """" &amp; " " &amp; """" &amp; file2path &amp; """"
      Dim cmdmods As String = " "
      Dim proc As New Process
      proc.StartInfo.CreateNoWindow = True
      proc.StartInfo.UseShellExecute = False
      proc.StartInfo.FileName = "C:\Windows\System32\cmd.exe"
      proc.StartInfo.Arguments = cmdinput &amp; cmdmods
      proc.StartInfo.RedirectStandardOutput = True
      proc.StartInfo.RedirectStandardError = True
      proc.StartInfo.RedirectStandardInput = True
      proc.Start()
      proc.StandardInput.WriteLine(cmdinput)
      proc.StandardInput.Close()
      scanbox.AppendText(proc.StandardOutput.ReadToEnd)
      proc.WaitForExit()
      proc.Close()
      proc.Dispose()
  End Sub
  </code></pre>
  
  <p>I Greatly appreciate the patience from  Hans Passant and Dan Verdolino in offering suggestions to my rambling question.  I've been hammering my head against a wall for a week trying to kludge together some way of doing this.</p>