如何从c#启动外部可执行文件并在进程结束时获取退出代码

可能重复:
如何从C#启动进程?

我想启动在命令行中运行的外部可执行文件来执行某项任务。 完成后,我想检查它返回的错误代码。 我该怎么做?

试试这个:

public virtual bool Install(string InstallApp, string InstallArgs) { System.Diagnostics.Process installProcess = new System.Diagnostics.Process(); //settings up parameters for the install process installProcess.StartInfo.FileName = InstallApp; installProcess.StartInfo.Arguments = InstallArgs; installProcess.Start(); installProcess.WaitForExit(); // Check for sucessful completion return (installProcess.ExitCode == 0) ? true : false; } 
  Process process = new Process(); process.StartInfo.FileName = "[program name here]"; process.StartInfo.Arguments = "[arguments here]"; process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; process.Start(); process.WaitForExit(); int code = process.ExitCode; 

您可以使用Process.Start()方法。

启动处理有实例和静态方法,具体取决于您的操作方式。

您可以在此处查看MSDN文档。 它将描述操作和监视从C#启动的外部进程所需的一切。

您可以使用Process类的Start static方法。 例如,要最小化Internet Explorer,请转到www.example.com:

 ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe", "www.example.com"); startInfo.WindowStyle = ProcessWindowStyle.Minimized; Process.Start(startInfo); 

关于返回值,如果操作成功,该方法将返回true,否则将引发Win32Exception。 您可以检查该类的NativeErrorCode成员以获取与该特定错误关联的Win32错误代码。