title: Automating interactive command line process from C#
body: <p>I have an *.exe that has an interactive text command interface which I want to automate from C#.</p>
  
  <p>The (1) is the C # result, and the (2) is the result of directly using *.exe.
  (latter miss "connect ok!" &amp; "download successfully!")</p>
  
  <p>(1)
  connect ok!
  cable detect ok!
  download successfully!</p>
  
  <p>(2)
  cable detect ok!</p>
  
  <p>I don’t know how to make the result of C # the same as the (2), below is my code:</p>
  
  <pre><code>Process p = new Process();
  p.StartInfo.FileName = ex.exe; 
  p.StartInfo.UseShellExecute = false;
  p.StartInfo.RedirectStandardInput = true;
  p.StartInfo.RedirectStandardOutput = true;
  p.StartInfo.RedirectStandardError = true;
  p.StartInfo.CreateNoWindow = true;
  
  p.Start();
  p.StandardInput.WriteLine("prj_project open " + "...../bin/store//*****Water.ldf");
  p.StandardInput.WriteLine("pgr_project open " + "...../bin/store/imp11/imp11.xcf");
  p.StandardInput.WriteLine("pgr_program run");
  p.StandardInput.WriteLine("exit");
  
  p.WaitForExit();
  
  var str = p.StandardOutput.ReadToEnd();
  var str2 = p.StandardError.ReadToEnd();
  Console.WriteLine(str);
  Console.WriteLine(str2);
  p.Close();
  </code></pre>
  
  <p>For some reason, I want to mask out some places, hope not to cause your confusion.</p>
  
  <p>If you can help me solve this problem, thank you very much.</p>
  
  <p>It drives me crazy...It took me almost fifteen hours to solve...</p>