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

use*_*949 10 .net c#

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

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

use*_*779 17

试试这个:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

  • 您知道那个“布尔表达式”吗?true:false和`«boolean expression»`是否等效? (3认同)

Bra*_*uce 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;
Run Code Online (Sandbox Code Playgroud)