2011-06-28 54 views
6

如何在C#中運行控制檯應用程序,將參數傳遞給它,並在Unicode中獲取應用程序的結果?在控制檯應用程序中使用了Console.WriteLine。 重要的一點是在控制檯應用程序中編寫Unicode。在C#中使用參數運行控制檯應用程序

+0

很多帖子。該控制檯僅支持8位字符編碼。從技術上講,你可以將Console.OutputEncoding切換到utf8。如果你在沒有重定向的情況下運行它,這看起來不會很好。使用文件,而不是一個好主意。 –

回答

10

樣品從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(); 
4

退房Process.Start()

MSDN - Process.Start Method

您的代碼可能會看起來像:

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

process.WaitForExit(); 

var exitCode = process.ExitCode; 

如果「控制檯應用程序的結果」你的意思是程序的任何輸出到控制檯運行時...您需要查看文檔並找出如何將程序的輸出從控制檯重定向到另一個流。

1

看看Process這個課。您可以使用Process.Start(「myexe.exe」)調用任何可執行文件;

3

嘗試用下面的代碼,在這裏「 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); 
相關問題