26

I want to execute an application(command line application) from the C#... and I want after executing this application and providing the input to it, I want to parse the output that will result it. Since, it will output many things, and I want to take, just the things that I need it from it...

How can I do that ??

How can I get the output in the c# and then take only the things I need it from it ??

For executing the command line in C#... I'm thinking to use "Jeff MC" way, that he explained it in this thread How To: Execute command line in C#, get STD OUT results

Thanks alot

1 Answer 1

56

There is one more way of getting all the output as events as and when they are output by the other console application cmd_DataReceived gets raised whenever there is output and cmd_Error gets raised whenever there is an error raised in the other application.

If you want to parse the output, probably handling these events is a better way to read output and handle errors in the other application as and when they occur.

using System;
using System.Diagnostics;

namespace InteractWithConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
            cmdStartInfo.FileName = @"C:\Windows\System32\cmd.exe";
            cmdStartInfo.RedirectStandardOutput = true;
            cmdStartInfo.RedirectStandardError = true;
            cmdStartInfo.RedirectStandardInput = true;
            cmdStartInfo.UseShellExecute = false;
            cmdStartInfo.CreateNoWindow = true;

            Process cmdProcess = new Process();
            cmdProcess.StartInfo = cmdStartInfo;
            cmdProcess.ErrorDataReceived += cmd_Error;
            cmdProcess.OutputDataReceived += cmd_DataReceived;
            cmdProcess.EnableRaisingEvents = true;
            cmdProcess.Start();
            cmdProcess.BeginOutputReadLine();
            cmdProcess.BeginErrorReadLine();

            cmdProcess.StandardInput.WriteLine("ping www.bing.com");     //Execute ping bing.com
            cmdProcess.StandardInput.WriteLine("exit");                  //Execute exit.

            cmdProcess.WaitForExit();
        }

        static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine("Output from other process");
            Console.WriteLine(e.Data);
        }

        static void cmd_Error(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine("Error from other process");
            Console.WriteLine(e.Data);
        }
    }
}
6
  • Nice, didn't know about those events! :) Mar 20, 2011 at 9:52
  • 1
    I've found that for some processes it is better to execute the command this way to assure the process will exit after execution: cmdProcess.StandardInput.WriteLine("ping www.bing.com && exit");
    – Gobe
    Feb 5, 2015 at 19:23
  • Thanks @MarceloGobetti . See also stackoverflow.com/a/13719223/492 for && and other options in that context.
    – CAD bloke
    Aug 17, 2015 at 0:30
  • These also include in the e.Data the prompt from the command prompt, ie "C:\path\to\some\directory\ping www.bing.com"... is there a way just to get the result? Jun 1, 2021 at 14:46
  • @Amit why did you ping me?! If you meant to ask the author of this answer, he gets notification anyway without any ping. If you meant to ping other user who posted a comment here, you can write your comment again, pinging the correct user. :) Apr 19, 2022 at 6:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.