我想启动在命令行中运行的外部可执行文件来执行某项任务。完成后,我想检查它返回的错误代码。我该怎么办?
答案 0 :(得分:16)
试试这个:
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;
}
答案 1 :(得分:11)
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;
答案 2 :(得分:1)
答案 3 :(得分:0)
您可以使用Process类'Start static method。例如,要最小化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错误代码。