1

A Program I have to work with opens a CMD-window with a message after processing some data. The message is something like "finished processing" and I don't have access to the code behind the other Programm.

The goals I want to achieve is:

  1. Get the message from the CMD-window and write it to a text box (or similar) in my own programm.
  2. Close the CMD-window

I managed to attach the process to my programm using Process[] localByName = Process.GetProcessesByName("cmd"); but I can't manage to get the output.

Thank you for your help.

Edit: To elaborate a bit further about the circumstances of the problem: I start the other programm with a command line command. I pass some parameters and after a bit, the other programm opens a cmd window with the message. If i open the other programm normally, then the message is displayed within the application. None of the parameters give me the option to manipulate the output and i am not able to pipeline the output to another file.

As for why i have to do this: I was given the task to add quality of life improvements to the other programm and using the command line is the easiest way to access the functionality of the other programm. The other solution available would be to reverse engeneer the processing of the data.

1
  • "I was given the task to add quality of life improvements" such an improvement of your life probably would be to find a C#/.Net lib that does the same thing reliably and testable. :D Commented Jan 10, 2023 at 10:40

1 Answer 1

2

The Process class has a StandardOutput property of type StreamReader that you can use to read the output of the process. Using your example:

Process[] localByName = Process.GetProcessesByName("cmd");
if (localByName.Length > 0)
{
    var cmdProcess = localByName[0];
    var reader = cmdProcess.StandardOutput;
    var output = reader.ReadToEnd();

    Console.WriteLine($"The output from the cmd process is: {output}");
}

Once you are done with your message processing, you can use the CloseMainWindow() method of the Process class to close the cmd window:

cmdProcess.CloseMainWindow();

This answer is assuming that you want to capture the output of cmd after it has been attached to your process.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.