ProcessStartInfo调用实现对exe输出内容的获取

具体见代码操作

using System ;
using System .Diagnostics;
using System .IO;

class Program
{
    static void Main()
    {
        // 用ProcessStartInfo启动一个exe程序
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"xxxxx.exe"; //exe程序的路径
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;

        //启动调用
        using (Process process = Process.Start (start))
        {
            process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived );
            process.BeginOutputReadLine ();
            //等待退出 
            process.WaitForExit ();
        }
    }
    //委托接受exe的输出内容
    static void process_OutputDataReceived( object sender , DataReceivedEventArgs e)
    {
        if (null != e)
        {
            Console.WriteLine (e. Data);
        }
    }
}

你可能感兴趣的:(.NET)