Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a external executable from c# and get the exit code when the process ends [duplicate]

Tags:

c#

.net

Possible Duplicate:
How to start a process from C#?

I want to start a external executable running in command line to do some task. After it is done, I want to check the errorcode it returns. How can I do it?

like image 885
user496949 Avatar asked Dec 02 '25 06:12

user496949


2 Answers

Try this:

    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;
    }
like image 60
user507779 Avatar answered Dec 04 '25 19:12

user507779


        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;
like image 29
Brad Bruce Avatar answered Dec 04 '25 21:12

Brad Bruce