首頁 > 後端開發 > C++ > 如何在不使用文件的情況下從.NET應用程序中捕獲控制台輸出?

如何在不使用文件的情況下從.NET應用程序中捕獲控制台輸出?

Susan Sarandon
發布: 2025-01-29 12:16:10
原創
845 人瀏覽過

How Can I Capture Console Output from a .NET Application Without Using Files?

從.NET應用程序中檢索控制台輸出:無歸檔方法

在許多情況下,從.NET應用程序中運行控制台應用程序並捕獲其輸出很有用。 但是,避免使用臨時文件實現這一目標可能很棘手。 >

>

解決方案:利用

> ProcessStartInfo.RedirectStandardOutput密鑰是屬性。 將其設置為>將控制台應用程序的標準輸出流直接直接引導到您的.NET應用程序。

以下是一個代碼示例,展示了此技術:

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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板