我该如何在C#中运行控制台应用程序,传递参数,并以Unicode格式获取应用程序的结果?在控制台应用程序中使用
Console.WriteLine
。重要的一点是在控制台应用程序中写入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()
:
您的代码可能如下所示:
var process = Process.Start(pathToProgram, argsString);
process.WaitForExit();
var exitCode = process.ExitCode;
在这里,您可以了解如何从控制台应用程序中读取输出,您可以使用Process.Start()来启动它。
http://www.aspcode.net/ProcessStart-and-redirect-standard-output.aspx在编程中,你需要小心使用其他示例,因为它们可能存在问题。如果你想避免自己写代码时常见的错误,请阅读 "如何正确使用 System.Diagnostics.Process"。
如果你需要使用一个库,可以参考这个链接:http://csharptest.net/browse/src/Library/Processes,其中包含了一个简短的使用指南:"使用 ProcessRunner 类"。