Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a application to get or return an exit code

Tags:

c#

.net

How to get an exit code from an application. Example: I call: System.Diagnostic.Start("regsvr32", "mydll.dll"); How to get an exit code from regsvr32?

And how to write an application which returns an exit code (like regsvr32). Thanks.

I'am using .NET 4.0 & C#.

like image 335
Leo Vo Avatar asked Dec 07 '22 21:12

Leo Vo


2 Answers

This is actually two separate questions, but... The exit code is the value returned from the program's Main function. So:

public static int Main()
{
    // code

    return 0;
}

Will return an exit code of 0. Unless the preceding // code does something different.

Use the Process' ExitCode property to get this value from the application to execute.

0 is typically returned when the program succeeds. Anything else is a failure but this is a matter of interpretation.

like image 113
Paul Sasik Avatar answered Dec 18 '22 06:12

Paul Sasik


Something like this should be sufficient.

private int CallSomething()
{
    using ( var p = new Process() )
    {
        p.StartInfo = new ProcessStartInfo("RegSvr32");
        p.Start();

        p.WaitForExit();

        return p.ExitCode;
    }
}

p.ExitCode is the exit code of the called process.

like image 25
Uwe Keim Avatar answered Dec 18 '22 06:12

Uwe Keim