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

查看:120
本文介绍了如何解析从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...

我怎样才能做到这一点?

How can I do that ??

我怎样才能获得在C#中的输出,然后我需要从它只拿东西??

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

有关执行C#中的命令行......我想用杰夫MC的方式,他在此线程解释它
<一href=\"http://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results\">How TO:在C#中执行命令行,得到性病OUT的结果

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

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

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