163

我需要生成一个作为控制台应用程序的子进程,并捕获其输出。

我为方法编写了以下代码:

string retMessage = String.Empty;
ProcessStartInfo startInfo = new ProcessStartInfo();
Process p = new Process();

startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;

startInfo.UseShellExecute = false;
startInfo.Arguments = command;
startInfo.FileName = exec;

p.StartInfo = startInfo;
p.Start();

p.OutputDataReceived += new DataReceivedEventHandler
(
    delegate(object sender, DataReceivedEventArgs e)
    {
        using (StreamReader output = p.StandardOutput)
        {
            retMessage = output.ReadToEnd();
        }
    }
);

p.WaitForExit();

return retMessage;

但是,这不会返回任何内容。我不相信OutputDataReceived事件被回调,或者WaitForExit()命令可能阻塞线程,所以它永远不会回调。

有什么建议吗?

编辑:看起来我对回调太努力了。正在做:

return p.StandardOutput.ReadToEnd(); 

似乎工作正常。

4

9 回答 9

178

这是我已经验证可以工作的代码。我用它来生成 MSBuild 并监听它的输出:

process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
process.Start();
process.BeginOutputReadLine();
于 2008-11-12T23:40:46.017 回答
41

我刚刚尝试了这件事,以下对我有用:

StringBuilder outputBuilder;
ProcessStartInfo processStartInfo;
Process process;

outputBuilder = new StringBuilder();

processStartInfo = new ProcessStartInfo();
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.UseShellExecute = false;
processStartInfo.Arguments = "<insert command line arguments here>";
processStartInfo.FileName = "<insert tool path here>";

process = new Process();
process.StartInfo = processStartInfo;
// enable raising events because Process does not raise events by default
process.EnableRaisingEvents = true;
// attach the event handler for OutputDataReceived before starting the process
process.OutputDataReceived += new DataReceivedEventHandler
(
    delegate(object sender, DataReceivedEventArgs e)
    {
        // append the new data to the data already read-in
        outputBuilder.Append(e.Data);
    }
);
// start the process
// then begin asynchronously reading the output
// then wait for the process to exit
// then cancel asynchronously reading the output
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.CancelOutputRead();

// use the output
string output = outputBuilder.ToString();
于 2012-03-16T01:03:45.390 回答
29

这是一些完整而简单的代码来执行此操作。当我使用它时,它工作得很好。

var processStartInfo = new ProcessStartInfo
{
    FileName = @"C:\SomeProgram",
    Arguments = "Arguments",
    RedirectStandardOutput = true,
    UseShellExecute = false
};
var process = Process.Start(processStartInfo);
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

请注意,这仅捕获标准输出;它不捕获标准错误。如果两者都需要,请对每个流使用此技术

于 2013-08-30T09:46:09.900 回答
27

我需要同时捕获 stdout 和 stderr 并在进程未按预期退出时让它超时。我想出了这个:

Process process = new Process();
StringBuilder outputStringBuilder = new StringBuilder();

try
{
process.StartInfo.FileName = exeFileName;
process.StartInfo.WorkingDirectory = args.ExeDirectory;
process.StartInfo.Arguments = args;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.EnableRaisingEvents = false;
process.OutputDataReceived += (sender, eventArgs) => outputStringBuilder.AppendLine(eventArgs.Data);
process.ErrorDataReceived += (sender, eventArgs) => outputStringBuilder.AppendLine(eventArgs.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
var processExited = process.WaitForExit(PROCESS_TIMEOUT);

if (processExited == false) // we timed out...
{
    process.Kill();
    throw new Exception("ERROR: Process took too long to finish");
}
else if (process.ExitCode != 0)
{
    var output = outputStringBuilder.ToString();
    var prefixMessage = "";

    throw new Exception("Process exited with non-zero exit code of: " + process.ExitCode + Environment.NewLine + 
    "Output from process: " + outputStringBuilder.ToString());
}
}
finally
{                
process.Close();
}

我将 stdout 和 stderr 管道传输到同一个字符串中,但如果需要,您可以将其分开。它使用事件,所以它应该在它们到来时处理它们(我相信)。我已经成功运行了它,很快就会对其进行批量测试。

于 2015-07-29T14:09:13.710 回答
21

看起来你的两条线有问题。在设置事件处理程序以捕获输出之前启动该过程。在添加事件处理程序之前,该过程可能刚刚完成。

像这样换行。

p.OutputDataReceived += ...
p.Start();        
于 2008-11-13T00:34:06.190 回答
3

设置 StartInfo 后,您需要调用 p.Start() 来实际运行该过程。实际上,您的函数可能挂在 WaitForExit() 调用上,因为该过程实际上从未启动过。

于 2008-11-12T23:23:27.063 回答
3

重定向流是异步的,并且可能会在进程终止后继续。Umar 提到在进程终止后取消process.CancelOutputRead()。但是,这具有数据丢失的可能性。

这对我来说很可靠:

process.WaitForExit(...);
...
while (process.StandardOutput.EndOfStream == false)
{
    Thread.Sleep(100);
}

我没有尝试这种方法,但我喜欢 Sly 的建议:

if (process.WaitForExit(timeout))
{
    process.WaitForExit();
}
于 2014-01-31T14:29:45.893 回答
2

来自犹大的答案对我不起作用(或不完整),因为应用程序在第一次之后退出BeginOutputReadLine();

这对我来说是一个完整的片段,读取 ping 的恒定输出:

        var process = new Process();
        process.StartInfo.FileName = "ping";
        process.StartInfo.Arguments = "google.com -t";
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.OutputDataReceived += (sender, a) => Console.WriteLine(a.Data);
        process.Start();
        process.BeginOutputReadLine();
        process.WaitForExit();
于 2015-11-09T13:24:32.790 回答
-1

这是我用来运行进程并获取其输出和错误的方法:

public static string ShellExecute(this string path, string command, TextWriter writer, params string[] arguments)
    {
        using (var process = Process.Start(new ProcessStartInfo { WorkingDirectory = path, FileName = command, Arguments = string.Join(" ", arguments), UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true }))
        {
            using (process.StandardOutput)
            {
                writer.WriteLine(process.StandardOutput.ReadToEnd());
            }
            using (process.StandardError)
            {
                writer.WriteLine(process.StandardError.ReadToEnd());
            }
        }

        return path;
    }

例如 :

@"E:\Temp\MyWorkingDirectory".ShellExecute(@"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\svcutil.exe", Console.Out);
于 2013-02-18T08:35:13.070 回答