在许多情况下,从.NET应用程序中运行控制台应用程序并捕获其输出很有用。 但是,避免使用临时文件实现这一目标可能很棘手。>
>解决方案:利用
>
ProcessStartInfo.RedirectStandardOutput
密钥是
ProcessStartInfo.RedirectStandardOutput
true
此代码段执行C#编译器(
>
<code class="language-csharp">// Initiate a new process for the console application Process compiler = new Process(); // Configure process settings compiler.StartInfo.FileName = "csc.exe"; compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs"; // Enable output redirection compiler.StartInfo.UseShellExecute = false; compiler.StartInfo.RedirectStandardOutput = true; // Begin the process compiler.Start(); // Capture the output string output = compiler.StandardOutput.ReadToEnd(); // Display the captured output Console.WriteLine(output); // Wait for process completion compiler.WaitForExit();</code>重要说明:
要进行完整的输出捕获,请记住还要重定向标准错误流(csc.exe
)处理控制台应用程序生成的任何错误或警告。
以上是如何在不使用文件的情况下从.NET应用程序中捕获控制台输出?的详细内容。更多信息请关注PHP中文网其他相关文章!