Click here to Skip to main content
15,885,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I need to get output of command window , its already existing process , from this process i need to read output(command line output) by using c#.

Note: I am not starting any process from here. Process already executed in diff place and need to verify string from command line.

Regard's
Suri

What I have tried:

By using this i am getting output , i need without process start already running process need to get output data :

C#
string command = "/c " + richTextBox2.Text;

           ProcessStartInfo procStartInfo = new ProcessStartInfo("CMD", command);

           Process proc = new Process();
           proc.StartInfo = procStartInfo;
          // proc.Start();
           procStartInfo.RedirectStandardOutput = true;
           procStartInfo.UseShellExecute = false;
           proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
           proc.Start();
           proc.BeginOutputReadLine();
           proc.WaitForExit();



C#
void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
       {
           if (e.Data != null)
           {
               string newLine = e.Data.Trim() + Environment.NewLine;
             //  MethodInvoker append = () => richTextBox1.Text += newLine;
               MethodInvoker append = () => richTextBox1.AppendText(newLine);
               richTextBox1.BeginInvoke(append);
           }
       }
Posted
Updated 18-Oct-16 3:45am
Comments
[no name] 18-Oct-16 9:54am    
"I am not starting any process", sure looks like you are.
"Process already executed in diff place", what does "diff place" mean? A different method? A different program? A different country? A different planet?
Surendra Reddy V 19-Oct-16 0:41am    
Hi,

Diff palce means we are not starting process from Process.Start(), manually executed process and need to read consoleoutput from same.

Are you clear?

Thanks
Suri

1 solution

You can't do that. Youo can only get process output as it's happening from the process as it's running. You get that from the StandardOut variable. Read up on that here:

Process.StandardOutput Property (System.Diagnostics)[^]
 
Share this answer
 
v2
Comments
Surendra Reddy V 19-Oct-16 0:39am    
Hi John,

Thanks for quick reply.

Yes I know this ,process running time only we can read output from console.

Is there any other way for existing processes consoleoutput read for that i mentioned.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900