mdb*_*mdb 155
使用ProcessStartInfo.RedirectStandardOutput属性可以很容易地实现这一点.完整的样本包含在链接的MSDN文档中; 唯一需要注意的是,您可能还需要重定向标准错误流以查看应用程序的所有输出.
Process compiler = new Process();
compiler.StartInfo.FileName = "csc.exe";
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();
Console.WriteLine(compiler.StandardOutput.ReadToEnd());
compiler.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
Shi*_*hah 29
这比@mdb接受的答案有点改进.具体来说,我们还捕获过程的错误输出.此外,我们通过捕获这些事件,因为输出ReadToEnd的(),如果你想捕捉不起作用两个错误和常规输出.我花了很长时间才能完成这项工作,因为它实际上还需要在Start()之后调用BeginxxxReadLine().
using System.Diagnostics;
Process process = new Process();
void LaunchProcess()
{
process.EnableRaisingEvents = true;
process.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(process_OutputDataReceived);
process.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(process_ErrorDataReceived);
process.Exited += new System.EventHandler(process_Exited);
process.StartInfo.FileName = "some.exe";
process.StartInfo.Arguments = "param1 param2";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
//below line is optional if we want a blocking call
//process.WaitForExit();
}
void process_Exited(object sender, EventArgs e)
{
Console.WriteLine(string.Format("process exited with code {0}\n", process.ExitCode.ToString()));
}
void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data + "\n");
}
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data + "\n");
}
Run Code Online (Sandbox Code Playgroud)
Fra*_*nov 13
在创建控制台进程时,使用ProcessInfo.RedirectStandardOutput重定向输出.
然后,您可以使用Process.StandardOutput读取程序输出.
第二个链接有一个示例代码如何操作.
ConsoleAppLauncher是一个专门用于回答该问题的开源库.它捕获控制台中生成的所有输出,并提供启动和关闭控制台应用程序的简单界面.
每次控制台将新行写入标准/错误输出时,都会触发ConsoleOutput事件.这些行排队并保证遵循输出顺序.
也可以作为NuGet包.
获取完整控制台输出的示例调用:
// Run simplest shell command and return its output.
public static string GetWindowsVersion()
{
return ConsoleApp.Run("cmd", "/c ver").Output.Trim();
}
Run Code Online (Sandbox Code Playgroud)
实时反馈示例:
// Run ping.exe asynchronously and return roundtrip times back to the caller in a callback
public static void PingUrl(string url, Action<string> replyHandler)
{
var regex = new Regex("(time=|Average = )(?<time>.*?ms)", RegexOptions.Compiled);
var app = new ConsoleApp("ping", url);
app.ConsoleOutput += (o, args) =>
{
var match = regex.Match(args.Line);
if (match.Success)
{
var roundtripTime = match.Groups["time"].Value;
replyHandler(roundtripTime);
}
};
app.Run();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
113045 次 |
| 最近记录: |