Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, August 7, 2008 5:44 AM
Hi,
I am developing a C# console application which starts a commandline and gets some data through another command(which is irrelevant in this discussion). Eventually i will get some data in the command line and i just need only the last line of that.
I have used the following code:
Process.StartInfo.FileName = "cmd.exe";
Process.StartInfo.RedirectStandardInput = true;
Process.StartInfo.RedirectStandardOutput = true;
I have used Process.StandardInput.WriteLine to write the commands i need to the console.
But if Process.StartInfo.RedirectStandardOutput = true, I am not getting the required output using the statement
Process.StandardOutput.ReadToEnd().Split('\n');
Whereas ReadLine option is working fine and I am getting the first line of the text displayed.
Since everytime the data that i get on the console differs, I cannot hardcode any specific loaction from where to get my output.
Even if i put a watch on the statement Process.StandardOutput.ReadToEnd().Split('\n');
I am getting a Function timeout exception.
Kindly help me with this problem.
Regards
Gauri
All replies (7)
Thursday, August 7, 2008 6:51 AM ✅Answered
You also need to set Process.StartInfo.UseShellExecute to false.
/Ruben RJJournal
Thursday, August 7, 2008 7:24 AM ✅Answered
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("your process");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.CreateNoWindow = true;
System.IO.StreamWriter sIn;
System.IO.StreamReader sOut;
System.Diagnostics.Process objProcess;
objProcess = System.Diagnostics.Process.Start(psi);
// Attach the output for reading
sOut = objProcess.StandardOutput;
// Attach the in for writing
sIn = objProcess.StandardInput;
sIn.WriteLine("Write your command");
string strOutput = sOut.ReadToEnd();
// Close the io Streams;
sIn.Close();
sOut.Close();
objProcess.WaitForExit();
//take out the last line from strOutput
Hope this solve your issue
Regards,
Manju Sandhu
Thursday, August 7, 2008 7:51 AM ✅Answered
So I copied Manju's code... Myprocesss = "cmd.exe".
First, I sent the "ipconfig" command, with a readToEnd();
Timed out.
Tried reading in blocks, failed too.
Tried waitForInputIddle();, but forgot cmd has no graphical interface, failed too.
So I needed the cmd to end the stream if I wanted to ever read it... Ok so exit?
So I tried sending ALL my commands, INCLUDING the exit command first, then reading.
sIn.WriteLine("ipconfig");
sIn.WriteLine("ipconfig /all");
sIn.WriteLine("exit");
string strOutPut = sOut.ReadToEnd();
Works like a charm. My ReadToEnd() returns a string in half a second, and I got all the info I need. ;-)
Could this be of any help to you, Gauri?
The improbable we do, the impossible just takes a little longer. - Steven Parker
Thursday, August 7, 2008 12:13 PM ✅Answered
ReadToEnd() is apt to cause deadlock, especially when you code it after a WaitForExit() or send a lot of input. The process is writing its output to a buffer, that buffer isn't very big (2KB I think). It you don't read the contents of this buffer, say wiith ReadLine(), the process will stall, waiting for the buffer to be emptied. Your program in turn will stall since WaitForExit() will never return or the WriteLine() call will stall since the process is no longer reading input.
If that's your scenario, you'll need to read asynchronously with BeginOutputReadLine().
Hans Passant.
Thursday, August 7, 2008 7:35 AM
The above is a very nice standard answer, however...
I don't think the "cmd" ends the stream. There is nothing being sent FROM the cmd-box to indicate that the stream is ended.
Consider a webpage you load, once the entire html-code has reached your pc, the stream is done. Same with reading a file, once you reached the last character, the stream is done. However with cmd, the stream isn't "done". It just waits for new input, then if you give it another command, it will have output again.
Hmm I seem to be having trouble expressing myself in English again, my apologies.
Anyways, I don't think theres anything you can do to use ReadToEnd() on a cmd-output-stream, unless you send it the "exit" command.
Have you tried combining the readline() with a. an analysis of the line (i.e. do you get empty lines), or b. the Process.WaitForIddleInput() command?
The improbable we do, the impossible just takes a little longer. - Steven Parker
Thursday, August 7, 2008 7:51 AM
Hi,
In case you are running cmd process. Do remeber to write "EXIT" in input stream
Regards,
Manju Sandhu
Thursday, August 7, 2008 7:52 AM
Manju Sandhu said:
Hi,
In case you are running cmd process. Do remeber to write "EXIT" in input stream
Regards,
Manju Sandhu
Lol, simultaniously... :D
The improbable we do, the impossible just takes a little longer. - Steven Parker