17

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?

Community
  • 1
  • 1
user496949
  • 83,087
  • 147
  • 309
  • 426
  • 5
    @John, this doesn't duplicate that because the other question doesn't ask about the exit code, nor do any of the answers demonstrate how to retrieve it. – Ben Voigt Nov 23 '10 at 01:03
  • 2
    Admittedly the starting of an app has been covered repeatedly, the capturing of the return value has not bee covered as often. I wouldn't classify this as a "BLATANT DUPLICATE" – Brad Bruce Nov 23 '10 at 01:28
  • 1
    @Brad: original question title was "start a external exxcutable question". Exit code is the most trivial part of it. – John Saunders Nov 23 '10 at 17:17
  • Your are 7k user. You must to know how to use search (and google), really. – abatishchev Jan 27 '13 at 09:58
  • 2
    I really don't get the need for the closing here - This question is one of the top google results for obtaining a return code from a command line operation now. It may be "trivial" but that question wasn't answered on the site at the time of asking and is clearly helpful to other users. – Kelly Robins Jun 14 '14 at 02:11

4 Answers4

21

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;
    }
Blachshma
  • 17,097
  • 4
  • 58
  • 72
user507779
  • 471
  • 1
  • 3
  • 10
  • 5
    Did you know that `«boolean expression» ? true : false` and `«boolean expression»` are equivalent? – binki Jun 01 '18 at 02:08
13
        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;
Brad Bruce
  • 7,638
  • 3
  • 39
  • 60
1

You can use the Process.Start() method.

There are both instance and static methods for launching the processing, depending on how you want to do it.

You can view MSDN documentation here. It will describe everything you need for manipulating and monitoring an external process launched from C#.

Andrii Kalytiiuk
  • 1,501
  • 14
  • 26
Alex Marshall
  • 10,162
  • 15
  • 72
  • 117
0

You can use the Process class' Start static method. For example, to start internet explorer minimized, and make it go to www.example.com :

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

Regarding the return value, if the operation is successful, the method will return true, otherwise, a Win32Exception will be raised. You can check the NativeErrorCode member of that class to get the Win32 error code associated with that specific error.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Daniel Perez
  • 4,292
  • 4
  • 29
  • 54