簡體   English   中英

使用參數在 C# 中運行控制台應用程序

[英]run console application in C# with parameters

如何在 C# 中運行控制台應用程序,向其傳遞參數,並在 Unicode 中獲取應用程序的結果? Console.WriteLine用於控制台應用程序。 重要的一點是在控制台應用程序中寫入 Unicode。

來自MSDN的示例

 // Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

試試下面的代碼,這里的“Amay”是一個參數。

 System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(@"E:\\ConsoleApplicationt\bin\Debug\ConsoleApplicationt.exe", "Amay");

 System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);

查看Process.Start()

MSDN - Process.Start 方法

您的代碼可能類似於:

var process = Process.Start(pathToProgram, argsString);

process.WaitForExit();

var exitCode = process.ExitCode;

如果“控制台應用程序的結果”是指程序運行時的任何 output 到控制台...您需要查看文檔並弄清楚如何將程序的 output 從控制台重定向到另一個stream。

在這里http://www.aspcode.net/ProcessStart-and-redirect-standard-output.aspx你可以看到如何從控制台應用程序中讀取 output 你從 Process.Start() 開始。

看看過程class。 您可以使用 Process.Start("myexe.exe"); 調用任何可執行文件;

根據您的使用情況,您應該小心,其他一些示例可能會出現問題。 對於編寫自己的代碼所犯的常見錯誤,請閱讀“如何正確使用 System.Diagnostics.Process

對於要使用的庫,這里有一個: http://csharptest.net/browse/src/Library/Processes以及簡要使用指南:“使用 ProcessRunner class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM