我需要生成一个作为控制台应用程序的子进程,并捕获其输出。
我为方法编写了以下代码:
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();
似乎工作正常。
答案 0 :(得分:145)
这是我已经验证可以使用的代码。我用它来产生MSBuild并听取它的输出:
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
process.Start();
process.BeginOutputReadLine();
答案 1 :(得分:33)
我刚尝试了这件事,以下内容对我有用:
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();
答案 2 :(得分:20)
我需要捕获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连接到同一个字符串中,但如果需要,可以将它分开。它使用事件,所以它应该处理它们(我相信)。我已成功运行,并将很快进行批量测试。
答案 3 :(得分:19)
看起来你的两条线路无序。您在设置事件处理程序以捕获输出之前启动该过程。在添加事件处理程序之前,该过程可能刚刚完成。
像这样切换线条。
p.OutputDataReceived += ...
p.Start();
答案 4 :(得分:17)
这里有一些完整而简单的代码。我使用它时工作正常。
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();
请注意,这仅捕获标准输出;它不会捕获标准的错误。如果您想要两者,请为每个流使用this technique。
答案 5 :(得分:3)
设置StartInfo后,需要调用p.Start()来实际运行该进程。实际上,您的函数可能挂在WaitForExit()调用上,因为该进程实际上从未启动过。
答案 6 :(得分:2)
重定向流是异步的,并且可能在进程终止后继续。 Umar提及在流程终止后取消process.CancelOutputRead()
。然而,这有可能造成数据丢失。
这对我来说是可靠的:
process.WaitForExit(...);
...
while (process.StandardOutput.EndOfStream == false)
{
Thread.Sleep(100);
}
我没有尝试这种方法,但我喜欢Sly的建议:
if (process.WaitForExit(timeout))
{
process.WaitForExit();
}
答案 7 :(得分:0)
犹大的回答对我不起作用(或者不完整),因为申请在第一次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();
答案 8 :(得分:-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);