使用参数在C#中运行控制台应用程序

如何在C#中运行控制台应用程序,将参数传递给它,并以Unicode格式获取应用程序的结果? Console.WriteLine用于控制台应用程序。 重要的一点是在Console Application中编写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(); 

查看Process.Start()

MSDN – Process.Start方法

您的代码可能类似于:

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

如果通过“控制台应用程序的结果”表示程序运行时程序的任何输出…您将需要查看文档并找出如何将程序的输出从控制台重定向到另一个流。

尝试下面的代码,这里“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); 

这里http://www.aspcode.net/ProcessStart-and-redirect-standard-output.aspx您可以看到如何从控制台应用程序读取输出您从Process.Start()开始。

看一下Process类。 您可以使用Process.Start(“myexe.exe”)调用任何可执行文件;

根据您的使用情况,您应该小心,其他一些示例可能会有问题。 对于编写自己的代码时常见的错误,请阅读“ 如何正确使用System.Diagnostics.Process ”

对于要使用的库,有一个库: http : //csharptest.net/browse/src/Library/Processes ,其中包含一个简短的使用指南:“ 使用ProcessRunner类 ”