Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse command line output from c#?

Tags:

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

like image 676
Q8Y Avatar asked Mar 20 '11 08:03

Q8Y


People also ask

How are command line arguments parsed?

Argument Parsing using sys. Your program will accept an arbitrary number of arguments passed from the command-line (or terminal) while getting executed. The program will print out the arguments that were passed and the total number of arguments. Notice that the first argument is always the name of the Python file.

How do I run a command line argument in C++?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.

How does Getopt work in C?

The getopt() function is a builtin function in C and is used to parse command line arguments. Syntax: getopt(int argc, char *const argv[], const char *optstring) optstring is simply a list of characters, each representing a single character option.

What is parse output?

A Parse transformation creates the output fields that the parse asset specifies. The type and number of output fields depends on the parsing operations that the user configures in the parse asset.


1 Answers

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);         }     } } 
like image 183
Sanjeevakumar Hiremath Avatar answered Nov 09 '22 05:11

Sanjeevakumar Hiremath