2011-06-28 138 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); 
相关问题