Write an application to receive or return an exit code

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

And how to write an application that returns an exit code (e.g. regsvr32). Thanks.

I am using .NET 4.0 and C #.

+4
source share
6 answers

These are actually two separate questions, but ... The exit code is the value returned from the main function of the program. So:

public static int Main() { // code return 0; } 

It will return an exit code of 0. If the previous // code does not do something else.

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

0 usually returns when the program terminates successfully. Everything else is a failure, but it is a matter of interpretation.

+4
source

Something like this should be enough.

 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.

+4
source

Here is a helper class that shows you how to get the exit code, as well as the output stream and error stream. Perhaps this is what you are looking for.

 /// <summary> /// Run an executable. /// </summary> /// <param name = "executablePath">Path to the executable.</param> /// <param name = "arguments">The arguments to pass along.</param> /// <param name = "workingDirectory">The directory to use as working directory when running the executable.</param> /// <returns>A RunResults object which contains the output of the executable, plus runtime information.</returns> public static RunResults RunExecutable( string executablePath, string arguments, string workingDirectory ) { RunResults runResults = new RunResults(); if ( File.Exists( executablePath ) ) { using ( Process proc = new Process() ) { proc.StartInfo.FileName = executablePath; proc.StartInfo.Arguments = arguments; proc.StartInfo.WorkingDirectory = workingDirectory; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.OutputDataReceived += ( o, e ) => runResults.Output.Append( e.Data ).Append( Environment.NewLine ); proc.ErrorDataReceived += ( o, e ) => runResults.ErrorOutput.Append( e.Data ).Append( Environment.NewLine ); proc.Start(); proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); proc.WaitForExit(); runResults.ExitCode = proc.ExitCode; } } else { throw new ArgumentException( "Invalid executable path.", "executablePath" ); } return runResults; } public class RunResults { public int ExitCode; public StringBuilder Output = new StringBuilder(); public StringBuilder ErrorOutput = new StringBuilder(); } 

To return the exit code yourself, simply return an integer from your main () method.

+3
source

You can return an integer from the Main () method in a console application, for example.

 static int Main(string[] args) { try { //do some stuff return 0; //everything is good } catch //you will want more specific error-handling, catch-all is for example only { return 1; //something blew up! } } 
+2
source
+1
source

It looks like the System.Diagnostic.Start method returns the System.Diagnostic.Process class. This class has an ExitCode property that you can use to get the return value.

+1
source

All Articles