如何解析c#的命令行输出? [英] How to parse command line output from c#?

查看:23
本文介绍了如何解析c#的命令行输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 C# 执行一个应用程序(命令行应用程序).​​..并且我想在执行这个应用程序并向它提供输入之后,我想解析将产生它的输出.因为,它会输出很多东西,我想从中取出我需要的东西......

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...

我该怎么做??

如何在 c# 中获取输出,然后只从中取出我需要的东西??

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

为了在 C# 中执行命令行......我正在考虑使用Jeff MC"的方式,他在这个线程中解释了它如何:在 C# 中执行命令行,获取标准输出结果

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

非常感谢

推荐答案

当其他控制台应用程序 cmd_DataReceived 输出时,还有一种方法可以将所有输出作为事件获取只要有输出,并且 cmd_Error 就会在其他应用程序中出现错误时引发.

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:WindowsSystem32cmd.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);
        }
    }
}

这篇关于如何解析c#的命令行输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆