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

15
我该如何在C#中运行控制台应用程序,传递参数,并以Unicode格式获取应用程序的结果?在控制台应用程序中使用Console.WriteLine。重要的一点是在控制台应用程序中写入Unicode。

很多帖子。控制台仅支持8位字符编码。从技术上讲,您可以将Console.OutputEncoding切换为utf8。如果您在没有重定向的情况下运行它,那看起来不会很好。使用文件代替是个好主意。 - Hans Passant
6个回答

20

来自 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();

5

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

5

请查看 Process.Start()

MSDN - Process.Start 方法

您的代码可能如下所示:

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

process.WaitForExit();

var exitCode = process.ExitCode;

如果您所说的“控制台应用程序的结果”是指程序在运行时向控制台输出的任何内容...您需要查看文档并找出如何将程序的输出从控制台重定向到另一个流。

3

1

看一下Process类。你可以使用Process.Start("myexe.exe");启动任何可执行文件。


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接