繁体   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