Send/wait/receive data from CLI app through C# app on VS 2010

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a .exe which when opened looks something like this:
<ol>
Enter A: (waits input for A) Enter B: (wait input for B once A was entered) Doing calculations..Almost done..The sum of A + B is 10 Press enter to exit..</ol>
I want to access this .exe from my C# application, enter A and B (which are entered in two textboxes) and then get the result from step 5. Here is what I tried so far:

<pre class="lang-cs prettyprint
Code:
        ProcessStartInfo cmdStartInfo = new ProcessStartInfo();<br/>        cmdStartInfo.FileName = @"c:myTestService.exe";<br/>        cmdStartInfo.RedirectStandardOutput = true;<br/>        cmdStartInfo.RedirectStandardError = true;<br/>        cmdStartInfo.RedirectStandardInput = true;<br/>        cmdStartInfo.UseShellExecute = false;<br/>        cmdStartInfo.CreateNoWindow = false;<br/><br/>        Process cmdProcess = new Process();<br/>        cmdProcess.StartInfo = cmdStartInfo;<br/>        cmdProcess.ErrorDataReceived += cmd_Error;<br/>        cmdProcess.OutputDataReceived += cmd_DataReceived;<br/>        cmdProcess.EnableRaisingEvents = true;<br/>        cmdProcess.Start();<br/>        cmdProcess.BeginOutputReadLine();<br/>        cmdProcess.BeginErrorReadLine();<br/>        cmdProcess.StandardInput.WriteLine(textBox1.Text);<br/>        cmdProcess.StandardInput.WriteLine(textBox2.Text);<br/><br/>        static void cmd_DataReceived(object sender, DataReceivedEventArgs e)<br/>        {<br/>            MessageBox.Show("Output from other process: " + e.Data);<br/>        }<br/><br/>        static void cmd_Error(object sender, DataReceivedEventArgs e)<br/>        {<br/>            MessageBox.Show("Error from other process: " + e.Data);<br/>        }<br/>
[/code]
Im missing something though as this doesnt work as expected.
How do I:

Enter A Once A is entered, enter B When the console app returns "The sum of A + B is 10" how do I get value 10?How do I send Enter as a parameter to force the app to close?
<br/>

View the full article
 
Back
Top