回答
您可以使用Process类执行命令行命令。
以下代码将标准输出捕获到output
,并将进程退出代码分配到exitCode
。
using (Process p = new Process())
{
p.StartInfo.FileName = exeName;
p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
int exitCode = p.ExitCode;
}
我也将编写控制台应用程序,如何在控制台应用程序中设置exitCode?只需在完成执行后返回代码吧? – 001
只需从Main()返回错误级别代码。请参阅http://msdn.microsoft.com/en-us/library/0fwzzxz2.aspx。 –
我假设您使用Process
类来调用命令行应用程序。
您可以使用Process.ExitCode
找到该过程的退出代码。在启动它之前,您可以通过设置ProcessStartInfo.RedirectStandardOutput
来重定向其标准输出,然后使用Process.StandardOutput
或Process.OutputDataReceived
事件。
喜欢的东西:
Process mycommand = new Process();
mycommand.StartInfo.FileName = "myexe.exe";
mycommand.StartInfo.Arguments = "param1";
mycommand.StartInfo.UseShellExecute = false;
mycommand.StartInfo.RedirectStandardOutput = true;
mycommand.Start();
Console.WriteLine(mycommand.StandardOutput.ReadToEnd());
mycommand.WaitForExit();
通常你确定羯羊退出代码的EXE的状态是0,但可以说是下降到EXE
的作家看看这个问题enter link description here。
您可能需要的附加信息是process.ExitCode
以查看它是否成功。当然,控制台应用程序的主要方法必须在不成功时返回退出代码,而许多代码则不会。
为此,您使用Process.Start
方法。您可以控制如何处理与传递ProcessStartInfo
运行:
var myProcess = Process.Start(new ProcessStartInfo {
FileName = "process.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
});
if (!myProcess.WaitForExit(5000)) { // give it 5 seconds to exit
myProcess.Kill();
}
if (myProcess.ExitCode != 0) {
// error!
}
var output = myProcess.StandardOutput.ReadToEnd(); // access output
- 1. C程序的命令行执行
- 2. 如何从C++程序执行命令行命令
- 3. 运行命令行程序
- 4. 在c程序中执行SET命令
- 5. C++命令行程序设计UI?
- 6. 在C++中调用命令行程序
- 7. 通过C程序运行BASH命令
- 8. 从C程序执行pi命令
- 9. 运行在C Linux命令++程序
- 10. C# - 命令行应用程序调用
- 11. 在c程序中运行shell命令
- 12. 如何调试C#命令行程序
- 13. 第三方Windows命令行程序?
- 14. 我该如何测试执行命令行程序的PHP类方法?
- 15. 调用命令行程序
- 16. 运行命令来自C#程序的提示命令
- 17. C#方法行程序
- 18. 编译Java程序的命令行
- 19. 分配python命令行应用程序的更好方法
- 20. 暂停命令行python程序的最简单方法?
- 21. 一种使用Air 2创建命令行程序的方法?
- 22. 退出命令行程序的正确方法?
- 23. 命令行安装程序
- 24. 运行msi安装程序命令行
- 25. 转:命令行应用程序安装
- 26. 在C运行时与命令行程序交互(stdin/out)C
- 27. 通过命令行从java运行一个c/C++程序
- 28. 在C#应用程序中使用命令行程序
- 29. Python命令行应用程序
- 30. 以编程方式执行shell命令
的[捕获.NET中的控制台输出(C#)](可能重复http://stackoverflow.com/questions/186822/capturing-the -console -input-in-net-c) –