在許多情況下,從.NET應用程序中運行控制台應用程序並捕獲其輸出很有用。 但是,避免使用臨時文件實現這一目標可能很棘手。 >
>解決方案:利用
>
ProcessStartInfo.RedirectStandardOutput
密鑰是
ProcessStartInfo.RedirectStandardOutput
true
此代碼段執行C#編譯器(
>
// 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();
要進行完整的輸出捕獲,請記住還要重定向標準錯誤流(csc.exe
)處理控制台應用程序生成的任何錯誤或警告。
以上是如何在不使用文件的情況下從.NET應用程序中捕獲控制台輸出?的詳細內容。更多資訊請關注PHP中文網其他相關文章!